Java mouse listener isn't working

不打扰是莪最后的温柔 提交于 2021-01-29 18:04:26

问题


I'm studying Java and learning how to use mouse listener. However here is a very simple code which doesn't work.

import acm.program.*;
import java.awt.event.*;

public class Test extends GraphicsProgram{

    public void run() {
        isMouseClicked= false;
        addMouseListeners();
        while (true) {
            if (isMouseClicked) {
                println ("OK");
                break;
            }
        }
    }

    public void mouseClicked(MouseEvent e) {
        isMouseClicked= true;
    }

    private boolean isMouseClicked;

}

The idea is very simple. "isMouseClicked" is false at the beginning, and once mouse is clicked, it turns to true and print "OK" in the screen. The problem I have is that, if I run in a normal mode, no matter how I click mouse, it will not hit and print "OK". However if I run it in the debug-mode. After I clicked mouse, put a breakpoint on

if (isMouseClicked);

Then it turns out that it is true and "OK" is printed. Can anyone tell me why is that? Many thanks in advance.

来源:https://stackoverflow.com/questions/38464303/java-mouse-listener-isnt-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!