use*_*388 3 java user-interface swing windowlistener
我有一个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);
}
Run Code Online (Sandbox Code Playgroud)