一开始的时候只是想写个JFrame,让他在关闭的时候提示一下,于是重写了,WindowAdapter的windowClosing方法,可是失败了,窗口最后还是关闭了,无论你是不是真的要关闭,于是写了下面的程序,发现,setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)使我的程序不好使了,他自动的做关闭的动作,JFrame和Frame不一样,所以,让setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE),然后重新写就好使了。
//这个是测试setDefaultCloseOperationWindowConstants.DO_NOTHING_ON_CLOSE)的
import java.awt.event.*;
import javax.swing.*;
public class tryWindowAdapter extends JFrame {
public tryWindowAdapter() {
super("try to close") ;
this.setSize(400,400) ;
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) ;
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.out.println("haha") ;
System.exit(0) ;
}
}
);
this.setVisible(true) ;
}
public static void main(String args[]) {
new tryWindowAdapter() ;
}
}
//关闭时提示
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.event.*;
public class tryWindowAdapter extends JFrame {
public tryWindowAdapter() {
super("try to close") ;
this.setSize(400,400) ;
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) ;
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog((Frame)e.getSource(),"really quit?")) {
System.exit(0) ;
}else
return ;
}
}
);
this.setVisible(true) ;
}
public static void main(String args[]) {
new tryWindowAdapter() ;
}
}