JOptionPane 打开时是否可以暂停当前线程?

Is it possible to pause current thread while JOptionPane is open?

我正在 Java 开发一款游戏,我正试图在 JOptionPane 确认框打开时暂停它。这是代码:

window.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent event) {

            int dialogResult =  JOptionPane.showConfirmDialog (null, "Would You Like to exit this game? Your progress will not be saved!","Warning",JOptionPane.YES_NO_OPTION);
            //What should i do with this???
            try {
                Thread currentThread = Thread.currentThread();
                synchronized(currentThread) {
                    currentThread.wait();
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ////
            if(dialogResult == JOptionPane.YES_OPTION){
                exitProcedure();
            }
            else {
                if(dialogResult == JOptionPane.NO_OPTION || dialogResult == JOptionPane.CLOSED_OPTION) {

                    Thread currentThread = Thread.currentThread();
                    synchronized(currentThread) {

                        currentThread.notify();
                    }

                }
            }

        }
    });

这样做正确吗?我应该怎么做才能让它发挥作用?

Swing 事件在 Event Dispatcher Thread 中处理,您不希望以任何方式暂停此线程。它处理所有 UI 操作,如重绘,因此任何暂停都会使您的应用程序无响应。

您必须获取对线程的引用,该线程是 运行 后台逻辑,然后暂停它。尽管您没有提供完整的源代码,但很可能不是 Thread.currentThread()。您可以使用 SwingUtils.isEventDispatchThread() 方法检查线程类型。