我有一个 Java 桌面应用程序,我希望当用户选择“退出”时弹出一个窗口,询问他是否要继续关闭该应用程序。我知道如何打开窗口并读取用户的响应,但我需要知道如何阻止应用程序关闭(类似于 System.close().cancel()
)。
这可能吗?
最佳答案
是的,这是可能的。
调用 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
后,添加 WindowListener
或 WindowAdapter
并在 windowClosing(WindowEvent)
方法中,弹出一个 JOptionPane
.
int result = JOptionPane.showConfirmDialog(frame, "Exit the application?");
if (result==JOptionPane.OK_OPTION) {
System.exit(0);
}
关于java - 如何停止关闭 Java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469097/