简体   繁体   English

从按钮调用时鼠标事件等待冻结

[英]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.

相关问题 使用wait和notifyAll时Java程序冻结 - Java program freezes when using wait and notifyAll 当工具提示超过按钮时,鼠标事件不起作用 - Mouse event not working when tool tips come over button 按住鼠标按钮时,将忽略JavaFX MousedMoved事件 - JavaFX MousedMoved event ignored when mouse button is held down 从主线程调用IPC到服务的调用会导致NullPointerException,但不会在按钮事件上调用 - IPC call to service causes NullPointerException when called from main thread but not on button event 在 CoroutineDispatcher 中调用 CoroutineDispatcher 时应用程序冻结 - App freezes when CoroutineDispatcher is called inside a CoroutineDispatcher 为辅助鼠标按钮Javafx添加鼠标事件 - Adding mouse event for secondary mouse button Javafx 从鼠标侦听器方法调用时repaint()不起作用 - repaint() not working when called from mouse listener method JavaFX Node.snapshot()从工作线程调用时冻结线程,想法? - JavaFX Node.snapshot() freezes thread when called from worker thread, ideas? 鼠标脱离JTable时发生鼠标事件? - mouse event when mouse is getting out of JTable? 当按下按钮时,ProgressIndicator将冻结。 JavaFX - ProgressIndicator freezes when button is pressed. JavaFX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM