如何在Swing应用程序中使用退出按钮? [英] How to use Exit button in Swing Application?
问题描述
我是使用Swing开发Java Form Application的新手。退出应用程序的退出按钮的合适代码是什么?
I am new in Java Form Application development using Swing. What is the suitable code for exit button that will exit the application?
例如:
在C#(.NET Framework)中我们使用 Application.Exit();
作为退出什么按钮的代码。 Java中退出应用程序的等效代码是什么?
For example:
In C# (.NET Framework) we use Application.Exit();
as the code for exit what button. What is the equivalent code in Java to exit an application?
推荐答案
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
现在进行一些解释。与问题中最接近的等价物是..
Now for a bit of explanation. The closest equivalent to as seen in the question is..
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
但这会杀死整个JRE,而不管正在运行的其他非守护程序线程如何。如果它们正在运行,则应该关闭它们或明确清理它们。
But that kills the entire JRE irrespective of other non-daemon threads that are running. If they are running, they should be shut down or cleaned up explicitly.
如果没有其他非守护程序线程在运行, DISPOSE_ON_CLOSE
将处置当前的 JFrame
并结束虚拟机。
If there are no other non-daemon threads running, DISPOSE_ON_CLOSE
will dispose the current JFrame
and end the virtual machine.
这篇关于如何在Swing应用程序中使用退出按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!