我想窗体上单击右上角的关闭按钮时出现“确定要退出吗?”的对话框,当点击“否”时不退出程序,点击“是”退出程序。但是使用下面代码点击“否”后还是关闭了程序主窗体但并未退出程序?请高手指点。 this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
int x=JOptionPane.showConfirmDialog(null,"确定要退出吗?", "系统退出提 示!",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE); if(x==0)
System.exit(0);
}
});
public void windowClosing(WindowEvent e){
int x=JOptionPane.showConfirmDialog(null,"确定要退出吗?", "系统退出提 示!",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE); if(x==0)
System.exit(0);
}
});
or
JDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)而且这句应该改一下,最好用定义的常量来代替而不是直接写个0
if(x==0)
System.exit(0);
改成:if(x==JOptionPane.YES_OPTION)
System.exit(0);