eclipse 如何在 Swing 应用程序中使用退出按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25446660/
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
How to use Exit button in Swing Application?
提问by Sondhi
I am new in Java Form Application development using Swing. What is the suitable code for exit button that will exit the application?
我是使用 Swing 进行 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?
例如:在 C# (.NET Framework) 中,我们使用Application.Exit();退出按钮的代码。Java 中退出应用程序的等效代码是什么?
回答by Andrew Thompson
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);
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.
但这会杀死整个 JRE,而不管其他正在运行的非守护进程线程如何。如果它们正在运行,则应明确关闭或清理它们。
If there are no other non-daemon threads running, DISPOSE_ON_CLOSEwill dispose the current JFrameand end the virtual machine.
如果没有其他非守护线程在运行,DISPOSE_ON_CLOSE将处理当前JFrame并结束虚拟机。
回答by predi
@AndrewThompson is right about JFrame.EXIT_ON_CLOSEbut you can take steps to avoid just killing stuff.
@AndrewThompson 是对的,JFrame.EXIT_ON_CLOSE但您可以采取措施避免杀死东西。
JFrame.EXIT_ON_CLOSEis just fine, if you handle cleanup in an overridden JFrame.processWindowEvent(WindowEvent)checking for WindowEvent.WINDOW_CLOSINGevents.
JFrame.EXIT_ON_CLOSE很好,如果您在覆盖JFrame.processWindowEvent(WindowEvent)的WindowEvent.WINDOW_CLOSING事件检查中处理清理。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
public class ExitJFrame extends JFrame {
public ExitJFrame() {
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Exit");
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ExitJFrame.this.processWindowEvent(
new WindowEvent(
ExitJFrame.this, WindowEvent.WINDOW_CLOSING));
}
});
setSize(200, 200);
setLocationRelativeTo(null);
}
@Override
protected void processWindowEvent(WindowEvent e) {
// more powerful as a WindowListener, since you can consume the event
// if you so please - asking the user if she really wants to exit for
// example, do not delegate to super if consuming.
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
doCleanup();
super.processWindowEvent(e);
} else {
super.processWindowEvent(e);
}
}
private void doCleanup() {
final JDialog dialog = new JDialog(this, true);
dialog.setLayout(new BorderLayout());
dialog.setUndecorated(true);
JProgressBar progress = new JProgressBar();
dialog.add(progress);
dialog.add(new JLabel("Waiting for non-daemon threads to exit gracefully..."), BorderLayout.NORTH);
progress.setIndeterminate(true);
Timer timer = new Timer(2000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
dialog.pack();
dialog.setLocationRelativeTo(this);
dialog.setVisible(true);
}
public static void main (String[] args) {
new ExitJFrame().setVisible(true);
}
}
回答by Eyadkht
If you want to create a button which exits the application when the user clicks it, try this:
如果您想创建一个在用户单击时退出应用程序的按钮,请尝试以下操作:
JButton exit = new JButton("exit");
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
exit.addActionListener(al);
Now when you press on that button the application will exit.
现在,当您按下该按钮时,应用程序将退出。
回答by chiastic-security
If you're talking about a JButtonthat you want to put into your form, then you want to invoke JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)in your constructor for the form, and then your button handler can invoke
如果您正在谈论JButton要放入表单中的 a,那么您想要JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)在表单的构造函数中调用,然后您的按钮处理程序可以调用
dispose()
to close the form.
关闭表单。
回答by Krishna
Very simple answer is as @Andrew Thompson said.
正如@Andrew Thompson 所说,非常简单的答案。
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
OR
或者
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
and if u want to make it in other way by code. then u can put this code anywhere. in events.
如果你想通过代码以其他方式制作它。然后你可以把这个代码放在任何地方。在事件中。
System.exit(0);
System.exit(0);
hope this will help.
希望这会有所帮助。

