containerlistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 536 行 · 第 1/2 页
HTML
536 行
<body onload="load()"> <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl> <div id=TopBar_right> <a target="_blank" href="http://java.sun.com/javase/6/download.jsp">Download the JDK</a> <br> <a href="../../search.html" target="_blank">Search the Tutorials</a> <br> <a href="javascript:toggleLeft()" id="ToggleLeft">Hide the TOC</a> </div> </div> </div> </div> </div> </div> <div class=PrintHeaders> <b>Trail:</b> Creating a GUI with JFC/Swing <br><b>Lesson:</b> Writing Event Listeners <br><b>Section:</b> Implementing Listeners for Commonly Handled Events </div> <div id=LeftBar class=LeftBar_shown> <div id=Contents> <div class="linkLESSON"><a href="index.html">Writing Event Listeners</a></div><div class="linkAHEAD"><a href="intro.html">Introduction to Event Listeners</a></div><div class="linkAHEAD"><a href="generalrules.html">General Information about Writing Event Listeners</a></div><div class="linkAHEAD"><a href="eventsandcomponents.html">Listeners Supported by Swing Components</a></div><div class="linkAHEAD"><a href="handling.html">Implementing Listeners for Commonly Handled Events</a></div><div class="linkBHEAD"><a href="actionlistener.html">How to Write an Action Listener</a></div><div class="linkBHEAD"><a href="caretlistener.html">How to Write a Caret Listener</a></div><div class="linkBHEAD"><a href="changelistener.html">How to Write a Change Listener</a></div><div class="linkBHEAD"><a href="componentlistener.html">How to Write a Component Listener</a></div><div class="nolinkBHEAD">How to Write a Container Listener</div><div class="linkBHEAD"><a href="documentlistener.html">How to Write a Document Listener</a></div><div class="linkBHEAD"><a href="focuslistener.html">How to Write a Focus Listener</a></div><div class="linkBHEAD"><a href="internalframelistener.html">How to Write an Internal Frame Listener</a></div><div class="linkBHEAD"><a href="itemlistener.html">How to Write an Item Listener</a></div><div class="linkBHEAD"><a href="keylistener.html">How to Write a Key Listener</a></div><div class="linkBHEAD"><a href="listdatalistener.html">How to Write a List Data Listener</a></div><div class="linkBHEAD"><a href="listselectionlistener.html">How to Write a List Selection Listener</a></div><div class="linkBHEAD"><a href="mouselistener.html">How to Write a Mouse Listener</a></div><div class="linkBHEAD"><a href="mousemotionlistener.html">How to Write a Mouse-Motion Listener</a></div><div class="linkBHEAD"><a href="mousewheellistener.html">How to Write a Mouse-Wheel Listener</a></div><div class="linkBHEAD"><a href="propertychangelistener.html">How to Write a Property Change Listener</a></div><div class="linkBHEAD"><a href="tablemodellistener.html">How to Write a Table Model Listener</a></div><div class="linkBHEAD"><a href="treeexpansionlistener.html">How to Write a Tree Expansion Listener</a></div><div class="linkBHEAD"><a href="treemodellistener.html">How to Write a Tree Model Listener</a></div><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=componentlistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=documentlistener.html>Next »</a> </div> <div id=PageTitle>How to Write a Container Listener</div> <blockquote>Container events are firedby a <code>Container</code> just after a componentis added to or removed from the container.These events are for notification only —no container listener need be present for components to be successfully added or removed.<p>The following example demonstrates container events.By clicking <b>Add a button</b> or <b>Remove a button</b>,you can add buttons to or remove them froma panel at the bottom of the window.Each time a button is added to or removed from the panel,the panel fires a container event,and the panel's container listener is notified.The listener displays descriptive messagesin the text area at the top of the window.<p><p><center><IMG SRC="../../figures/uiswing/events/ContainerEventDemo.png" WIDTH="362" HEIGHT="364" ALIGN="BOTTOM" ALT=""></center></p><p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/events/examples/ContainerEventDemo.jnlp">Run ContainerEventDemo</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#ContainerEventDemo">example index</a>.<li> Click the button labeled <b>Add a button</b>. <br> You'll see a button appear near the bottom of the window. The container listener reacts to the resulting component-added event by displaying "JButton #1 was added to javax.swing.JPanel" at the top of the window.<li> Click the button labeled <b>Remove a button</b>. <br> This removes the most recently added button from the panel, causing the container listener to receive a component-removed event.</ol><hr></blockquote><p>You can find the demo's code in <a class="SourceLink" target="_blank" href="examples/ContainerEventDemo.java"><code>ContainerEventDemo.java</code></a>.Here is the demo's container event handling code:<blockquote><pre>public class ContainerEventDemo ... implements ContainerListener ... { <em>...//where initialization occurs:</em> buttonPanel = new JPanel(new GridLayout(1,1)); buttonPanel.addContainerListener(this); ... public void componentAdded(ContainerEvent e) { displayMessage(" added to ", e); } public void componentRemoved(ContainerEvent e) { displayMessage(" removed from ", e); } void displayMessage(String action, ContainerEvent e) { display.append(((JButton)e.getChild()).getText() + " was" + action + e.getContainer().getClass().getName() + newline); } ...}</pre></blockquote></blockquote><h3><a name="api">The Container Listener API</a></h3><blockquote><p align=center><a name="containerlistener">The ContainerListener Interface</a><p><em>The corresponding adapter class is<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/ContainerAdapter.html"><code>ContainerAdapter</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/java/awt/event/ContainerListener.html#componentAdded(java.awt.event.ContainerEvent)">componentAdded(ContainerEvent)</a></td><td>Called just after a component is added to the listened-to container.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/ContainerListener.html#componentRemoved(java.awt.event.ContainerEvent)">componentRemoved(ContainerEvent)</a></td><td>Called just after a component is removed from the listened-to container.</td></tr></table><p align=center><a name="containerevent">The ContainerEvent Class</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/ContainerEvent.html#getChild()">Component getChild()</a></td><td>Returns the component whose addition or removal triggered this event.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/ContainerEvent.html#getContainer()">Container getContainer()</a></td><td>Returns the container that fired this event. You can use this instead of the <code>getSource</code> method.</td></tr></table></blockquote><a name="eg"><h3>Examples that Use Container Listeners</h3></a><blockquote>The following table lists theexamples that use container 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#ContainerEventDemo"><code>ContainerEventDemo</code></a></td><td> This section</td><td> Reports all container events that occur on a single panel to demonstrate the circumstances under which container events are fired.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=componentlistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=documentlistener.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 Component Listener <br><b>Next page:</b> How to Write a Document Listener </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?