java 中关于关闭按扭事件控制是否关闭的解决

在右上角的关闭按钮事件处理中,可进行这样的方式事件监听方式。

this.addWindowListener(new WindowListener() {
			
			public void windowOpened(WindowEvent e) {
			}
			public void windowIconified(WindowEvent e) {
			}
			public void windowDeiconified(WindowEvent e) {
			}
			public void windowDeactivated(WindowEvent e) {
			}
			public void windowClosing(WindowEvent e) {
				
				int i = JOptionPane.showConfirmDialog(null, "是否保存所做的修改", "提示", JOptionPane.YES_NO_CANCEL_OPTION);
				if(i == JOptionPane.YES_OPTION ){
			        System.out.println("保存了");
					db.savecouData(cous);
				    db.savestuData(stus);
				    System.exit(0);
				}else if(i == JOptionPane.NO_OPTION){
					System.exit(0);
				}
			}

 

但在这里面这样做法有时可能会不成功,原因在于一开始的时候

this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

 这个地方可能被设为

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 这可能是一种习惯性动作,注意下就行了

 

以上的代码是自己做的一个程序中的一部份,这个类继承了JFrame了,所以用this.

你可能感兴趣的:(java)