internalframelistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 627 行 · 第 1/2 页
HTML
627 行
<div class="linkBHEAD"><a href="treeselectionlistener.html">How to Write a Tree Selection Listener</a></div><div class="linkBHEAD"><a href="treewillexpandlistener.html">How to Write a Tree-Will-Expand Listener</a></div><div class="linkBHEAD"><a href="undoableeditlistener.html">How to Write an Undoable Edit Listener</a></div><div class="linkBHEAD"><a href="windowlistener.html">How to Write Window Listeners</a></div><div class="linkAHEAD"><a href="api.html">Listener API Table</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Event-Handling Problems</a></div></div> </div> <div id=MainFlow class=MainFlow_indented> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> > <a href=index.html target=_top>Writing Event Listeners</a> </span> <div class=NavBit> <a target=_top href=focuslistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=itemlistener.html>Next »</a> </div> <div id=PageTitle>How to Write an Internal Frame Listener</div> <blockquote>An <code>InternalFrameListener</code> is similar to a<code>WindowListener</code>. Like the window listener,the internal frame listener listens for events thatoccur when the "window" has been shown for the first time,disposed of, iconified, deiconified, activated, or deactivated.Before using an internal frame listener, please familiarize yourself withthe <code>WindowListener</code> interface in<a href="windowlistener.html">How to Write Window Listeners</a>.<p>The application shown in the following figuredemonstrates internal frame events.The application listens for internal frame eventsfrom the Event Generator frame,displaying a message that describes each event.<p><center><IMG SRC="../../figures/uiswing/events/InternalFrameEventDemo.png" WIDTH="510" HEIGHT="333" ALIGN="BOTTOM" ALT="Screendump of InternalFrameEventDemo"></center></p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/events/examples/InternalFrameEventDemo.jnlp">Run InternalFrameEventDemo</a> using <a href=http://java.sun.com/products/javawebstart> Java<sup><font size=-2>TM</font></sup> Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#InternalFrameEventDemo">example index</a>.<li> Bring up the Event Generator internal frame by clicking the <b>Show internal frame</b> button. <br> You should see an "Internal frame opened" message in the display area.<li> Try various operations to see what happens. For example, click the Event Generator so that it gets activated. Click the Event Watcher so that the Event Generator gets deactivated. Click the Event Generator's decorations to iconify, maximize, minimize, and close the window. <br> See <a href="windowlistener.html">How to Write Window Listeners</a> for information on what kinds of events you'll see.</ol><hr></blockquote>Here is the internal frame event handling code:<blockquote><pre>public class InternalFrameEventDemo ... implements InternalFrameListener ... { ... public void internalFrameClosing(InternalFrameEvent e) { displayMessage("Internal frame closing", e); } public void internalFrameClosed(InternalFrameEvent e) { displayMessage("Internal frame closed", e); listenedToWindow = null; } public void internalFrameOpened(InternalFrameEvent e) { displayMessage("Internal frame opened", e); } public void internalFrameIconified(InternalFrameEvent e) { displayMessage("Internal frame iconified", e); } public void internalFrameDeiconified(InternalFrameEvent e) { displayMessage("Internal frame deiconified", e); } public void internalFrameActivated(InternalFrameEvent e) { displayMessage("Internal frame activated", e); } public void internalFrameDeactivated(InternalFrameEvent e) { displayMessage("Internal frame deactivated", e); } void displayMessage(String prefix, InternalFrameEvent e) { String s = prefix + ": " + e.getSource(); display.append(s + newline); } public void actionPerformed(ActionEvent e) { if (SHOW.equals(e.getActionCommand())) { ... if (listenedToWindow == null) { listenedToWindow = new JInternalFrame("Event Generator", true, //resizable true, //closable true, //maximizable true); //iconifiable //We want to reuse the internal frame, so we need to //make it hide (instead of being disposed of, which is //the default) when the user closes it. listenedToWindow.setDefaultCloseOperation( WindowConstants.HIDE_ON_CLOSE); listenedToWindow.addInternalFrameListener(this); ... } } ... }}</pre></blockquote></blockquote><h3><a name="api">The Internal Frame Listener API</a></h3><blockquote><p align=center><a name="internalframelistener">The InternalFrameListener Interface</a><p><em>The corresponding adapter class is<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameAdapter.html"><code>InternalFrameAdapter</code></a>.</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/javax/swing/event/InternalFrameListener.html#internalFrameOpened(javax.swing.event.InternalFrameEvent)">internalFrameOpened(InternalFrameEvent)</a></td><td> Called just after the listened-to internal frame 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/javax/swing/event/InternalFrameListener.html#internalFrameClosing(javax.swing.event.InternalFrameEvent)">internalFrameClosing(InternalFrameEvent)</a></td><td> Called in response to a user request that the listened-to internal frame be closed. By default, <code>JInternalFrame</code> hides the window when the user closes it. You can use the <code>JInternalFrame</code> <code>setDefaultCloseOperation</code> method to specify another option, which must be either <code>DISPOSE_ON_CLOSE</code> or <code>DO_NOTHING_ON_CLOSE</code> (both defined in <code>WindowConstants</code>, an interface that <code>JInternalFrame</code> implements). Or by implementing an <code>internalFrameClosing</code> method in the internal frame's listener, you can add custom behavior (such as bringing up dialogs or saving data) to internal frame closing.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameListener.html#internalFrameClosed(javax.swing.event.InternalFrameEvent)">internalFrameClosed(InternalFrameEvent)</a></td><td> Called just after the listened-to internal frame has been disposed of.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameEvent.html#internalFrameIconified(javax.swing.event.InternalFrameEvent)">internalFrameIconified(InternalFrameEvent)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameEvent.html#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)">internalFrameDeiconified(InternalFrameEvent)</a></td><td> Called just after the listened-to internal frame is iconified or deiconified, respectively.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameListener.html#internalFrameActivated(javax.swing.event.InternalFrameEvent)">internalFrameActivated(InternalFrameEvent)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameListener.html#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)">internalFrameDeactivated(InternalFrameEvent)</a></td><td> Called just after the listened-to internal frame is activated or deactivated, respectively.</td></tr></table><p>Each internal frame event method has a single parameter: an<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameEvent.html"><code>InternalFrameEvent</code></a> object.The <code>InternalFrameEvent</code> class defines no generallyuseful methods. To get the internal frame that fired the event,use the <code>getSource</code> method,which <code>InternalFrameEvent</code> inherits from<code>java.util.EventObject</code>.</blockquote><a name="eg"><h3>Examples that Use Internal Frame Listeners</h3></a><blockquote>No other source files currently contain internal frame listeners.However, internal frame listeners are very similar to<code>WindowListener</code>s and several Swing programs havewindow listeners:<p><table><tr><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#InternalFrameEventDemo"> <code>InternalFrameEventDemo</code></a></td><td> This section</td><td> Reports all internal frame events that occur on one internal frame to demonstrate the circumstances under which internal frame events are fired.</td></tr><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#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></table> </blockquote> <div class=NavBit> <a target=_top href=focuslistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=itemlistener.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 a Focus Listener <br><b>Next page:</b> How to Write an Item Listener </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?