首页 > 代码库 > addWindowListener -> WindowAdapter -> windowClosing
addWindowListener -> WindowAdapter -> windowClosing
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.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。