我有一个简单的java GUI应用程序,它会在退出程序之前提示用户输入"你确定要退出吗?"这样的消息.虽然这仅在我使用我的退出程序JButton时有效,但是当我在JFrame标题栏中使用红叉时,如果我在消息对话框窗口中单击是或否,则无关紧要.
对于此任务,我使用此代码向我的JFrame添加了一个新的WindowListener
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
int Answer = JOptionPane.showConfirmDialog(frame, "You want to quit?", "Quit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (answer == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
Run Code Online (Sandbox Code Playgroud)
如果我单击"否",程序将退出,我该如何停止该操作?