我无法在Java中的if语句中访问实例变量

时间:2019-02-12 07:27:13

标签: java class graphics

我正在尝试简化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);}

    public Button(Point p, Point p2, String text, String icon) { //Here, both the text and Icon is specified
        this.button = new JButton(text);
        this.setIcon(icon);
        this.setBounds(p, p2);
        this.button.addMouseListener(this);
        this.setBorderVisible(false);}

    public void draw(GraphWin win) {
        win.window.add(this.button);}
    public void setBounds(Point p, Point p2) {
        this.button.setBounds(p.x, p.y, p2.x - p.x, p2.y - p.y);
    }

    public void setEnabled(boolean enable) {
        this.button.setEnabled(enable);}
    public void disable() {
        this.button.setEnabled(false);}
    public void enable() {
        this.button.setEnabled(true);
    }

    public void setColor(Color color) {
        this.button.setBackground(color);}
    public void setColor(String color) {
        this.button.setBackground(Color.decode(color));}

    public void setText(String text) {
        this.button.setText(text);}
    public void setIcon(String icon) {
        File imageCheck = new File(icon);
        if(!imageCheck.exists()) 
            System.out.println("Image file not found!");
        else
            this.button.setIcon(new ImageIcon(icon));
    }

    public void resizeIcon(String icon, int width, int height) {
        Image img = new ImageIcon(icon).getImage();
        img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        this.button.setIcon(new ImageIcon(img));
    }

    public void setCustomMargins(int top, int bottom, int left, int right) {
        this.button.setMargin(new Insets(top, left, bottom, right));}
    public void setMargins(int m) {
        this.button.setMargin(new Insets(m, m, m, m));}

    public void setLabel(String label) {
        this.button.setToolTipText(label);
    }
    public void setBorderVisible(boolean border) {
        this.button.setBorderPainted(border);}

    public void setOpaque(boolean opaque) {
        this.button.setContentAreaFilled(opaque);}

    @Override
    public void mouseEntered(MouseEvent arg0) {
        this.hovering = true;
        System.out.println(1);
        }

    @Override
    public void mouseExited(MouseEvent arg0) {
        this.hovering = false;
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        this.pressed = true;
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        this.pressed = false;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        this.clicked = true;
        System.out.println(1);
    }

}

2 个答案:

答案 0 :(得分:1)

这类事情通常与线程有关。

Swing中的事件在AWT事件调度线程(EDT)上调度。为了确保线程安全,几乎所有与Swing / AWT有关的操作都应在EDT上完成。

在您的情况下,设置和读取的变量之间没有任何类型的锁定。添加println会导致暂停(带有各种内存屏障或其他障碍),该暂停使程序可以按期望的顺序运行。

您可能已经看到main个方法被编写为将执行直接传递给AWT。

class MyGUI {
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(MyGUI::go);
    }
    private static void go() {
        ...

最好自己提供主类,实现该主类以将应用程序类作为参数并在所有设置完成后传递执行。传统上,命令行使用main静态方法/函数,而其他子类型:Applet,Servlet等。

答案 1 :(得分:0)

最好的方法是使用isHovering()方法,但是对于有或没有Sysout的while(true)内部行为的有根据的猜测可能与编译器的优化有关。可以通过将悬停变量设置为transient

来解决