windowlistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 978 行 · 第 1/3 页
HTML
978 行
System.out.println(msg); } String convertStateToString(int state) { if (state == Frame.NORMAL) { return "NORMAL"; } if ((state & Frame.ICONIFIED) != 0) { return "ICONIFIED"; } //MAXIMIZED_BOTH is a concatenation of two bits, so //we need to test for an exact match. if ((state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) { return "MAXIMIZED_BOTH"; } if ((state & Frame.MAXIMIZED_VERT) != 0) { return "MAXIMIZED_VERT"; } if ((state & Frame.MAXIMIZED_HORIZ) != 0) { return "MAXIMIZED_HORIZ"; } return "UNKNOWN"; } public static void main(String[] args) { ... //Create and set up the window. frame = new JFrame("WindowEventDemo"); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); ... }}</pre></blockquote></blockquote><h3><a name="api">The Window Listener API</a></h3><blockquote>The following tables list the methods in the three windowlistener interfaces and the methods in the <code>WindowEvent</code>class. The methods from all three interfacesare available through the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowAdapter.html"><code>WindowAdapter</code></a> class. The window listener API falls into four categories:<ul><li><a href="#windowlistener">The WindowListener Interface</a><li><a href="#windowfocuslistener">The WindowFocusListener Interface</a><li><a href="#windowstatelistener">The WindowStateListener Interface</a><li><a href="#windowevent">The WindowEvent Class</a></ul><p align=center><a name="windowlistener">The WindowListener Interface</a><p><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowOpened(java.awt.event.WindowEvent)">windowOpened(WindowEvent)</a></td><td> Called just after the listened-to window has been shown for the first time.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowClosing(java.awt.event.WindowEvent)">windowClosing(WindowEvent)</a></td><td> Called in response to a user request that the listened-to window be closed. To actually close the window, the listener should invoke the window's <code>dispose</code> or <code>setVisible(false)</code> method.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowClosed(java.awt.event.WindowEvent)">windowClosed(WindowEvent)</a></td><td> Called just after the listened-to window has closed.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowIconified(java.awt.event.WindowEvent)">windowIconified(WindowEvent)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowDeiconified(java.awt.event.WindowEvent)">windowDeiconified(WindowEvent)</a></td><td> Called just after the listened-to window is iconified or deiconified, respectively.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowActivated(java.awt.event.WindowEvent)">windowActivated(WindowEvent)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowDeactivated(java.awt.event.WindowEvent)">windowDeactivated(WindowEvent)</a></td><td> Called just after the listened-to window is activated or deactivated, respectively. These methods are not sent to windows that are not frames or dialogs. For this reason, we prefer the 1.4 <code>windowGainedFocus</code> and <code>windowLostFocus</code> methods to determine when a window gains or loses the focus.</td></tr></table><p align=center><a name="windowfocuslistener">The WindowFocusListener Interface</a><p><em>This interface was introduced in release 1.4.</em><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowGainedFocus(java.awt.event.WindowEvent)">windowGainedFocus(WindowEvent)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowLostFocus(java.awt.event.WindowEvent)">windowLostFocus(WindowEvent)</a></td><td> Called just after the listened-to window gains or loses the focus, respectively.</td></tr></table><p align=center><a name="windowstatelistener">The WindowStateListener Interface</a><p><em>This interface was introduced in release 1.4.</em><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowListener.html#windowStateChanged(java.awt.event.WindowEvent)">windowStateChanged(WindowEvent)</a></td><td> Called just after the listened-to window's state is changed by being iconified, deiconified, maximized, or returned to normal. The state is available through the <code>WindowEvent</code> as a bitwise mask. The possible values, defined in <code>java.awt.Frame</code>, are:<ul><li>NORMAL. Indicates that no state bits are set.<li>ICONIFIED<li>MAXIMIZED_HORIZ<li>MAXIMIZED_VERT<li>MAXIMIZED_BOTH. Concatenates <code>MAXIMIZED_HORIZ</code> and <code>MAXIMIZED_VERT</code>. A window manager may support <code>MAXIMIZED_BOTH</code>, while not supporting <code>MAXIMIZED_HORIZ</code> or <code>MAXIMIZED_VERT</code>. The<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html"><code>java.awt.Toolkit</code></a> method<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#isFrameStateSupported(int)"><code>isFrameStateSupported(int)</code></a> can be used to determine what states are supported by the window manager.</ul></td></tr></table><p align=center><a name="windowevent">The WindowEvent Class</a><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowEvent.html#getWindow()">Window getWindow()</a></td><td> Returns the window that fired the event. You can use this instead of the <code>getSource</code> method.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowEvent.html#getOppositeWindow()">Window getOppositeWindow()</a></td><td>Returns the other window involved in this focus or activation change. For a <code>WINDOW_ACTIVATED</code> or <code>WINDOW_GAINED_FOCUS</code> event, this returns the window that lost activation or the focus. For a <code>WINDOW_DEACTIVATED</code> or <code>WINDOW_LOST_FOCUS</code> event, this returns the window that gained activation or the focus. For any other type of <code>WindowEvent</code> with a Java application in a different VM or context, or with no other window, <code>null</code> is returned. This method was introduced in release 1.4.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowEvent.html#getOldState()">int getOldState()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/WindowEvent.html#getNewState()">int getNewState()</a></td><td>For <code>WINDOW_STATE_CHANGED</code> events these methods return the previous or new state of the window as a bitwise mask. These methods were introduced in release 1.4.</td></tr></table></blockquote><a name="eg"><h3>Examples that Use Window Listeners</h3></a><blockquote>The following table lists theexamples that use window listeners. <p> <table><tr valign=top><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr valign=top><td> <a href="examples/index.html#WindowEventDemo"><code>WindowEventDemo</code></a></td><td> This section</td><td> Reports all window events that occur on one window to demonstrate the circumstances under which window events are fired.</td></tr><tr valign=top><td> <a href="../components/examples/index.html#SliderDemo"> <code>SliderDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/slider.html">How to Use Sliders</a></td><td> Listens for window iconify and deiconify events, so that it can stop the animation when the window isn't visible.</td></tr><tr valign=top><td> <a href="examples/index.html#InternalFrameEventDemo"><code>InternalFrameEventDemo</code></a></td><td> <a href="internalframelistener.html"> How to Write an Internal Frame Listener</a></td><td> Reports all internal frame events that occur on one internal frame to demonstrate the circumstances under which internal frame events are fired. Internal frame events are similar to window events.</td></tr valign=top><tr valign=top><td> <a href="../components/examples/index.html#DialogDemo"> <code>DialogDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/generaltext.html">Text Component Features</a></td><td><a class="SourceLink" target="_blank" href="../components/examples/CustomDialog.java"><code>CustomDialog.java</code></a> uses <code>setDefaultCloseOperation</code> instead of a window listener to determine what action to take when the user closes the window.</td></tr><tr valign=top><td> <a href="../components/examples/index.html#Framework"> <code>Framework</code></a> </td><td>—</td><td> A demo that allows multiple windows to be created and destroyed.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=undoableeditlistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=api.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> How to Write an Undoable Edit Listener <br><b>Next page:</b> Listener API Table </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?