java 为什么关闭主窗口后我的应用程序仍然运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:37:17  来源:igfitidea点击:

Why does my application still run after closing main window?

javaswing

提问by ?ukasz Bownik

If I make a JFrame like this

如果我制作这样的 JFrame

public static void main(String[] args) {

     new JFrame().setVisible(true);
}

then after closing the window the appication doesn't stop (I need to kill it).

然后关闭窗口后,应用程序不会停止(我需要杀死它)。

What is the proper way of showing application's main windows ?

显示应用程序主窗口的正确方法是什么?

I'd also like to know a reason of a proposed solution.

我还想知道提出解决方案的原因。

Thanks in advance.

提前致谢。

回答by Burkhard

You should call the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);in your JFrame.

您应该setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);在 JFrame 中调用。

Example code:

示例代码:

public static void main(String[] args) {
    Runnable guiCreator = new Runnable() {
        public void run() {
            JFrame fenster = new JFrame("Hallo Welt mit Swing");
            fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fenster.setVisible(true);
        }
    };
    SwingUtilities.invokeLater(guiCreator);
}

回答by Yuval

There's a difference between the application window and the application itself... The window runs in its own thread, and finishing main()will not end the application if other threads are still active. When closing the window you should also make sure to close the application, possibly by calling System.exit(0);

应用程序窗口和应用程序本身是有区别的... 窗口在自己的线程中运行,main()如果其他线程仍然处于活动状态,则完成不会结束应用程序。关闭窗口时,您还应该确保关闭应用程序,可能通过调用System.exit(0);

Yuval =8-)

尤瓦尔 =8-)

回答by jassuncao

You must dispose the frame, invoking the dispose method in your window listener or using setDefaultCloseOperation. For the argument of the last one, you can use two options:

您必须处置框架,在窗口侦听器中调用处置方法或使用 setDefaultCloseOperation。对于最后一个的论点,您可以使用两个选项:

DISPOSE_ON_CLOSEor EXIT_ON_CLOSE.

DISPOSE_ON_CLOSEEXIT_ON_CLOSE

DISPOSE_ON_CLOSEonly dispose the frame resources.

DISPOSE_ON_CLOSE只处理帧资源。

EXIT_ON_CLOSEdisposes the frame resources and then invokes System.exit.

EXIT_ON_CLOSE处理框架资源,然后调用System.exit.

There is no real difference between the two unless you have non daemon threads. I prefer to use DISPOSE_ON_CLOSEbecause this way I'm able to notice if I forgot to terminate a thread, because the JVM will stop if there are no more threads running. That's also the reason closing a Frame without disposing will not terminate the application, since Swing creates a thread to handle events that is terminated only when dispose is invoked.

除非您有非守护进程线程,否则两者之间没有真正的区别。我更喜欢使用DISPOSE_ON_CLOSE这种方式,因为如果我忘记终止线程,我可以注意到,因为如果没有更多线程在运行,JVM 将停止。这也是关闭 Frame 而不进行 dispose 不会终止应用程序的原因,因为 Swing 创建了一个线程来处理仅在调用 dispose 时终止的事件。

回答by giannis christofakis

You can set a window listenerto the frame so the program terminates after you close it.

您可以为该框架设置一个窗口侦听器,以便程序在您关闭它后终止。

frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

回答by Steve Smith

The correct way to do this (unless you're writing a very trivial single-window app, i.e. no other windows or threads etc..) is to catch the windowClosing()event, and then call the dispose();method of the form.

执行此操作的正确方法(除非您正在编写一个非常简单的单窗口应用程序,即没有其他窗口或线程等)是捕获windowClosing()事件,然后调用dispose();表单的方法。

If your program doesn't completely exit after this, it means you have other non-deamon threads running, and you must stop these as best you see fit depending on your program.

如果您的程序在此之后没有完全退出,则意味着您有其他非守护线程正在运行,您必须根据您的程序尽可能地停止这些线程。

Calling System.exit()or setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);will force stop any other threads (and the whole program), meaning your code is less portable, and force-stopping threads is obviously dangerous (in a programming kind of way).

调用System.exit()setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);将强制停止任何其他线程(以及整个程序),这意味着您的代码可移植性较差,并且强制停止线程显然是危险的(以某种编程方式)。