Ent*_*ity 1 java swing jframe windowlistener
我有一个WorldEditor启动GameJFrame 的 JFrame。但是,当Game关闭时,我不希望它结束整个程序,所以我将默认关闭操作设置为HIDE_ON_CLOSE。WorldEditor但是,为了节省资源,我在运行时暂停Game。
如何检测Game窗口何时隐藏以便我可以恢复WorldEditor?
为什么不自己隐藏框架而不是使用默认框架HIDE_ON_CLOSE?
// inside WindowListener class\npublic windowClosing(WindowEvent e) {\n yourFrame.setVisible( false );\n // your code here...\n}\nRun Code Online (Sandbox Code Playgroud)\n\n编辑:来自文档:
\n\n\n\n\n默认关闭操作在任何窗口侦听器处理窗口关闭事件后执行。因此,例如,假设您指定默认关闭操作是处置帧。您还可以实现一个窗口侦听器,用于测试该帧是否是最后一个可见的帧,如果是,则保存一些数据并退出应用程序。在这些条件下,当用户关闭框架时,将首先调用窗口侦听器。如果它不退出应用程序,则将执行默认的关闭操作 \xe2\x80\x94 处理框架\n \xe2\x80\x94。
\n
带有工作示例的新编辑:
\n\nimport java.awt.event.*;\nimport javax.swing.JFrame;\n\npublic class ListenerTest extends JFrame implements WindowListener {\n\npublic static void main(String[] args) {\n ListenerTest frame = new ListenerTest();\n frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);\n frame.setVisible( true );\n}\n\npublic ListenerTest() {\n this.addWindowListener( this );\n}\n\npublic void windowActivated(WindowEvent e) {\n System.out.println(" activated ");\n}\npublic void windowClosed(WindowEvent e){\n System.out.println(" closed ");\n}\npublic void windowClosing(WindowEvent e){\n System.out.println(" closing ");\n}\npublic void windowDeactivated(WindowEvent e){\n System.out.println(" deactivated ");\n}\npublic void windowDeiconified(WindowEvent e){\n System.out.println(" deiconified ");\n}\npublic void windowIconified(WindowEvent e){\n System.out.println(" iconified ");\n}\npublic void windowOpened(WindowEvent e){\n System.out.println(" opened ");\n}\n}\nRun Code Online (Sandbox Code Playgroud)\n\n测试一下以捕获什么哪些事件正在触发。