码迷,mamicode.com
首页 > Windows程序 > 详细

addWindowListener -> WindowAdapter -> windowClosing

时间:2014-08-02 12:14:43      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   for   

Suppose we have a frame

final JFrame jframe = new JFrame("Triangle 3");

 

To catch window closing events and quit, there are two ways (as I know):

Way1.

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  Explanation: Exit the system when the window is closed.

 

Way2. 

jframe.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        jframe.dispose();  // to release resources for OpenGL
        System.exit(0);
    }
});

  Explanation (reference):

    给窗口注册一个事件监听器,
    当你点了窗口右上方的叉号时
    此方法被调用

    若对语法不懂,学习关于java inner class的知识

This catching action is necessary. The system would not quit even if the window has been closed without this event. You can try it yourself.

addWindowListener -> WindowAdapter -> windowClosing,布布扣,bubuko.com

addWindowListener -> WindowAdapter -> windowClosing

标签:style   blog   http   color   java   os   io   for   

原文地址:http://www.cnblogs.com/skyssky/p/3886547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!