Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 等待Swing GUI关闭,然后继续_Java_Multithreading_Swing_Command Line - Fatal编程技术网

Java 等待Swing GUI关闭,然后继续

Java 等待Swing GUI关闭,然后继续,java,multithreading,swing,command-line,Java,Multithreading,Swing,Command Line,我正在编写一个应用程序,要求用户在Swing GUI中输入一些数据,然后应用程序将使用这些数据。用户输入数据后,不再需要GUI,因为应用程序会将一些数据写入文件 总体思路是: launchGui(); closeGui(); continueWithComputation(); 我知道Swing在后台使用了一些线程,这就是为什么程序在关闭GUI之前不会阻塞 在继续执行continueWithComputation()之前,是否可以以任何方式等待GUI关闭(使用dispose()关闭单个JFra

我正在编写一个应用程序,要求用户在Swing GUI中输入一些数据,然后应用程序将使用这些数据。用户输入数据后,不再需要GUI,因为应用程序会将一些数据写入文件

总体思路是:

launchGui();
closeGui();
continueWithComputation();
我知道Swing在后台使用了一些线程,这就是为什么程序在关闭GUI之前不会阻塞

在继续执行
continueWithComputation()
之前,是否可以以任何方式等待GUI关闭(使用
dispose()
关闭单个
JFrame

等待Swing GUI关闭,然后继续

使用模态对话框。有关更多详细信息,请参见以下内容:

在继续使用ContinueWithComputing()之前,是否可以以任何方式等待GUI关闭(使用dispose()关闭单个JFrame)

  • 用户操作添加

  • 从代码到调用
    JFrame#setVisible(false)
    ,然后您可以运行
    continueWithComputation()
    ,您必须通过
    系统关闭此JVM。退出(0)
    ,否则将留在PC的RAM中,直到重新启动或关机


我也遇到了类似的问题,但没有使用
closeGui()
方法,我使用了WindowListener和一些Java同步,得到了这个相对较短的代码片段:

AtomicBoolean closed = new AtomicBoolean(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
        synchronized(closed) {
            closed.set(true);
            closed.notify();
        }
        super.windowClosed(e);
    }
} );

frame.setVisible(true);
synchronized(closed) {
    while (!closed.get()) {
        closed.wait();
    }
}

// executes after the Frame has been disposed

我的建议是打开jFrameGUI作为模态,然后一切都将等待它从屏幕上消失。由于JFrame不能设置为模态,因此必须以JDialog的形式打开GUI框架。我正在使用:

public class frameMain extends JDialog{ 
请确保在使用的构造函数中:

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
无论何时需要启动GUI框架,都应使用:

frameMain frm= new frameMain ();
frm.setModal(true);
frm.setVisible(true);