有的时候经常会发生在组件中增加监听器之后,监听器并没有收到事件,解决方法有2种,第一种是将监听器增加到顶层容器中,这样时间就有效了,但我今天说的是第二种,将组件中的事件激活。
看第一代码: import java.awt.Frame; import java.awt.event.WindowEvent; import javax.swing.WindowConstants; @SuppressWarnings("serial") public class WindowEventsSample extends Frame implements WindowConstants { private int defaultCloseOperation = EXIT_ON_CLOSE; public WindowEventsSample() { super(); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new WindowEventsSample(); } @Override protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { switch (defaultCloseOperation) { case HIDE_ON_CLOSE: setVisible(false); break; case DISPOSE_ON_CLOSE: dispose(); break; case DO_NOTHING_ON_CLOSE: default: break; case EXIT_ON_CLOSE: System.exit(0); break; } } } public void setDefaultCloseOperation(int operation) { if (operation != DO_NOTHING_ON_CLOSE && operation != HIDE_ON_CLOSE && operation != DISPOSE_ON_CLOSE && operation != EXIT_ON_CLOSE) { throw new IllegalArgumentException("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE"); } if (this.defaultCloseOperation != operation) { if (operation == EXIT_ON_CLOSE) { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkExit(0); } } int oldValue = this.defaultCloseOperation; this.defaultCloseOperation = operation; firePropertyChange("defaultCloseOperation", oldValue, operation); } } public int getDefaultCloseOperation() { return defaultCloseOperation; } } 运行上边的代码,我们发现无论我们操作都不能关闭窗口,这里也是为什么我们说用Frame而不是JFrame,呵呵,JFrame已经将这一步做好了,原因是该组件的WindowEvent并没有被激活,看下边第二段代码: public WindowEventsSample() { super(); // 激活组件的按键、窗口、鼠标事件 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); }
看第一代码: import java.awt.Frame; import java.awt.event.WindowEvent; import javax.swing.WindowConstants; @SuppressWarnings("serial") public class WindowEventsSample extends Frame implements WindowConstants { private int defaultCloseOperation = EXIT_ON_CLOSE; public WindowEventsSample() { super(); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new WindowEventsSample(); } @Override protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { switch (defaultCloseOperation) { case HIDE_ON_CLOSE: setVisible(false); break; case DISPOSE_ON_CLOSE: dispose(); break; case DO_NOTHING_ON_CLOSE: default: break; case EXIT_ON_CLOSE: System.exit(0); break; } } } public void setDefaultCloseOperation(int operation) { if (operation != DO_NOTHING_ON_CLOSE && operation != HIDE_ON_CLOSE && operation != DISPOSE_ON_CLOSE && operation != EXIT_ON_CLOSE) { throw new IllegalArgumentException("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE"); } if (this.defaultCloseOperation != operation) { if (operation == EXIT_ON_CLOSE) { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkExit(0); } } int oldValue = this.defaultCloseOperation; this.defaultCloseOperation = operation; firePropertyChange("defaultCloseOperation", oldValue, operation); } } public int getDefaultCloseOperation() { return defaultCloseOperation; } } 运行上边的代码,我们发现无论我们操作都不能关闭窗口,这里也是为什么我们说用Frame而不是JFrame,呵呵,JFrame已经将这一步做好了,原因是该组件的WindowEvent并没有被激活,看下边第二段代码: public WindowEventsSample() { super(); // 激活组件的按键、窗口、鼠标事件 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); }