我似乎和大多数人的问题相反。我有以下相当标准的代码,用于查看用户是否想在关闭窗口之前进行一些保存:
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
boolean close = true;
// check some files, asking if the user wants to save
// YES and NO handle OK, but if the user hits Cancel on any file,
// I want to abort the close process
// So if any of them hit Cancel, I set "close" to false
if (close) {
frame.dispose();
System.exit(0);
}
}
});
无论我尝试什么,当我从windowClosing中出来时窗口总是关闭。将WindowAdapter更改为WindowListener没有任何区别。奇怪的是,文档明确表示:“如果程序在处理此事件时未显式隐藏或处理窗口,则窗口关闭操作将被取消”,但对我来说并不起作用。有没有其他处理框架上X的方法?TIA
close
变量的逻辑可能是错误的。由于它默认为 true,看起来你没有正确地重置它。在 if 语句之前,你是否打印出了该变量的值? - camickr