我正在尝试简化 Javax swing 图形类,以便其他人更容易进入 Java 图形,但我在测试它时遇到了问题。请记住,我是作为代码用户而不是开发人员编写 main 方法。我需要能改变类方法代码而不是主要方法的答案。
我的主要方法代码应该做的是当用户将鼠标悬停在按钮上时打印“悬停”。但是,当我在 if 语句之前添加一个 SOP 语句时,它可以工作......鼠标悬停的方法在 Button 类中。
这是我的主要方法代码 -
public static void main(String[] args) {
GraphWin win = new GraphWin(1000, 1000, "Graphics Window - Test");
win.show();
Button button = new Button(new Point(380, 300), new Point(620, 400));
button.draw(win);
enter code herewhile(true) {
//System.out.println(button.hovering);
if(button.hovering) {
System.out.println("hovering");
}
}
}
这是我的 Button 类代码 -
public class Button implements MouseListener{
public JButton button;
public boolean clicked = false, hovering = false, pressed = false;
public Button(Point p, Point p2) { //This is the default constructor of the button with only 2 points specified
this.button = new JButton();
this.setBounds(p, p2);
this.button.addMouseListener(this);
this.setBorderVisible(false);}
public Button(Point p, Point p2, String text) { //This constructor requires text to be displayed`enter code here`
this.button = new JButton(text);
this.setBounds(p, p2);
this.button.addMouseListener(this);
this.setBorderVisible(false);}
public Button(String icon, Point p, Point p2) { //This constructor sets an Icon for the button
this.button = new JButton();
this.setIcon(icon);
this.setBounds(p, p2);
this.button.addMouseListener(this);
this.setBorderVisible(false);}
}
拉莫斯之舞
PIPIONE
相关分类