问题
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