我似乎对大多数人都有相反的问题.我有以下非常标准的代码,以查看用户是否想在关闭窗口之前进行一些保存:
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);
}
}
});
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,当我离开windowClosing时窗口总是关闭.将WindowAdapter更改为WindowListener没有任何区别.有点奇怪的是,文档明确说明"如果程序在处理此事件时没有明确隐藏或处理窗口,窗口关闭操作将被取消",但它对我来说不起作用.还有其他方法可以处理框架上的x吗?TIA