1: import java.awt.event.WindowAdapter;
2: import java.awt.event.WindowEvent;
3:
4: import javax.swing.JFrame;
5: import javax.swing.JOptionPane;
6: import javax.swing.WindowConstants;
7:
8: public class TestJDialog {
9:
10: public static void main(String[] args) {
11: JOptionPane.showMessageDialog( null , "程序开始启动" );
12: // 要被内置类访问,所以定义成fianl
13: final JFrame f = new JFrame( "TestJDialog" );
14: // 当用户在点击JFrame上的关闭窗口按键时,JFrame会自动隐藏这个框架窗口,
15: // 但没有真正关闭这个窗口,还在内存中,我们要调用windowClosing,
16: // 或用下面的方法
17: f.setDefaultCloseOperation(WindowConstants. DO_NOTHING_ON_CLOSE );
18: f.addWindowListener( new WindowAdapter() {
19:
20: @Override
21: public void windowClosing(WindowEvent e) {
22: super .windowClosing(e);
23: int retval = JOptionPane.showConfirmDialog(f, "你真的要结束吗?" ,
24: "结束程序" , JOptionPane. YES_NO_OPTION );
25: if (retval == JOptionPane. YES_OPTION ) {
26:
System.exit(0);
27:
}
28:
}
29:
});
30:
f.setSize(200, 200);
31: f.setVisible( true );
32:
}
33:
34:
}