如下,在JFrame中输入代码:
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0){
int option=JOptionPane.showConfirmDialog(null, "确认退出吗?", "退出确认",
JOptionPane.YES_NO_OPTION);
if(option==0) System.exit(0);
}
});
发现无论点是或否都无法取消关闭,可以在之前插入一句
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
即可。