[英]Mouse event wait freezes when called from button
I have a method which brings up a transparent window overlay so I can indicate corner points of a rectangular onscreen area via clicks. 我有一种方法可以调出透明的窗口叠加层,因此可以通过单击来指示屏幕上矩形区域的角点。
public Point getClickPoint(){
JFrame frame = new JFrame("");
MyMouseListener mouseL = new MyMouseListener();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
makeTranslucent(frame, Float.valueOf(0.40f));
frame.setSize(toolkit.getScreenSize());
frame.setVisible(true);
frame.addMouseListener(mouseL);
while(!mouseL.done){
try {
Thread.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
When I call this in a normal way it works fine, but if I call it by a button press, then it hangs, doesn't register clicks and eventually freezes. 当我以通常的方式调用它时,它可以正常工作,但是如果我通过按下按钮来调用它,则它会挂起,不会注册点击并最终冻结。
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
getClickPoint();
}
}
Is this something to do with the fact this is called originally by an action listener? 这与动作侦听器最初调用它有关吗?
You are blocking the Event Dispatch Thread . 您正在阻止事件调度线程 。 Since the
actionPerformed
method will be called on the EDT, your while loop in getClickPoint
will prevent the EDT from processing any events (including the mouse events you are waiting for), causing your program to become unresponsive. 由于将在EDT上调用
actionPerformed
方法,因此您在getClickPoint
的while循环将阻止EDT处理任何事件(包括您正在等待的鼠标事件),从而导致程序无响应。
If you need to perform time expensive tasks (such as blocking), take a look at SwingWorker
. 如果您需要执行耗时的任务(例如阻塞),请查看
SwingWorker
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.