JAVA GUI学习 - 窗口【x】按钮关闭事件触发器:重写processWindowEvent(WindowEvent e)方法

 1 public class WindowListenerKnow extends JFrame
 2 {
 3     public WindowListenerKnow()
 4     {
 5         this.setBounds(300, 100, 400, 400);
 6         this.setTitle("我是测试【x】按钮关闭方法的");
 7         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 8     }
 9     public void init()
10     {
11         
12     }
13     public static void main(String[] args)
14     {
15         WindowListenerKnow windowListenerKnow = new WindowListenerKnow();
16         windowListenerKnow.setVisible(true);
17     }
18     
19     /**
20      * 这里需要重写窗口的事件中转方法,而不是实现WindowListener接口,因为程序时从这个方法processWindowEvent进入到窗口关闭事件的
21      */
22     @Override
23     protected void processWindowEvent(WindowEvent e)
24     {
25         //这里需要对进来的WindowEvent进行判断,因为,不仅会有窗口关闭的WindowEvent进来,还可能有其他的WindowEvent进来
26         if (e.getID() == WindowEvent.WINDOW_CLOSING)
27         {
28             int option = JOptionPane.showConfirmDialog(null, "是否关闭程序?", "程序退出提示", JOptionPane.OK_CANCEL_OPTION);
29             if (option == JOptionPane.OK_OPTION)
30             {
31                 super.processWindowEvent(e);
32             }
33             else {
34                 //用户选择不退出本程序,因此可以继续留在本窗口
35             }
36         }
37         else {
38             super.processWindowEvent(e);
39         }
40     }
41 }

 

posted @ 2013-09-22 10:13  魏飞  阅读(2592)  评论(0编辑  收藏  举报
你见

或者不见我
我就在那里
不悲不喜
你念

或者不念我
情就在那里
不来不去
你爱

或者不爱我
爱就在那里
不增不减
你跟

或者不跟我
我的手就在你的手里
不舍不弃
来我怀里
或者
让我住进你的心里
默然

相爱
寂静

喜欢
Mr Frank