如何停止关闭Java应用程序
我有一个Java桌面应用程序,我希望当用户选择Exit时,会出现一个弹出窗口,询问他是否要继续关闭应用程序。我知道如何打开窗口并读取用户的响应,但我需要知道的是如何阻止应用程序关闭(类似于如何停止关闭Java应用程序,java,swing,user-interface,windowlistener,Java,Swing,User Interface,Windowlistener,我有一个Java桌面应用程序,我希望当用户选择Exit时,会出现一个弹出窗口,询问他是否要继续关闭应用程序。我知道如何打开窗口并读取用户的响应,但我需要知道的是如何阻止应用程序关闭(类似于System.close().cancel()) 这可能吗?在您的JFrame上,您必须设置defaultCloseOperation: JFrame.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); 然后将弹
System.close().cancel()
)
这可能吗?在您的
JFrame
上,您必须设置defaultCloseOperation
:
JFrame.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
然后将弹出窗口的关闭操作设置为“关闭时退出”您可以添加一个窗口侦听器。(注意:
WindowAdapter
在java.awt.event
包中)
是的,这是可能的
调用setDefaultCloseOperation(关闭时不执行任何操作)
后,添加一个或WindowAdapter
,并在windowClosing(WindowEvent)
方法中,弹出一个
设置
setDefaultCloseOperation(JDialog.DO\u NOTHING\u ON\u CLOSE)后
在JFrame或JDialog中添加windows侦听器:
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
int result = JOptionPane.showConfirmDialog((Component) null, "Do u really want to exit ?!",
"Confirmation", JOptionPane.YES_NO_OPTION);
if (result == 0) {
System.exit(0);
} else {
}
}
});
可能重复的
int result = JOptionPane.showConfirmDialog(frame, "Exit the application?");
if (result==JOptionPane.OK_OPTION) {
System.exit(0);
}
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
int result = JOptionPane.showConfirmDialog((Component) null, "Do u really want to exit ?!",
"Confirmation", JOptionPane.YES_NO_OPTION);
if (result == 0) {
System.exit(0);
} else {
}
}
});