如何停止关闭Java应用程序
我有一个Java桌面应用程序,我希望当用户选择退出"以获取一个弹出窗口时,询问他是否要继续关闭该应用程序.我知道如何显示窗口并读取用户的响应,但我需要知道的是如何停止关闭应用程序(例如System.close().cancel()
).
I have a Java Desktop application and I would like when the user selects Exit to get a pop-up window that asks him if he wants to proceed with closing the application or not. I know how to make the window come up and read the user's response but what I need to know is how I can stop the application from closing (something like System.close().cancel()
).
有可能吗?
是的.
调用setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
后,添加 WindowListener
或WindowAdapter
并在windowClosing(WindowEvent)
方法中,弹出
After calling setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
, add a WindowListener
or WindowAdapter
and in the windowClosing(WindowEvent)
method, pop a JOptionPane
.
int result = JOptionPane.showConfirmDialog(frame, "Exit the application?");
if (result==JOptionPane.OK_OPTION) {
System.exit(0);
}