componentlistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 607 行 · 第 1/2 页
HTML
607 行
<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=changelistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=containerlistener.html>Next »</a> </div> <div id=PageTitle>How to Write a Component Listener</div> <blockquote>One or more component events are firedby a <code>Component</code> objectjust after the component is hidden, made visible, moved, or resized.An example of a component listenermight be in a GUI builder toolthat's displaying information about the size of the currently selected component,and that needs to know when the component's size changes.You shouldn't need to use component eventsto manage basic layout and rendering.<p>The component-hidden and component-shown eventsoccur only as the result of calls to a <code>Component</code>'s<code>setVisible</code> method.For example, a window might be miniaturized into an icon (iconified)without a component-hidden event being fired.<p>The following example demonstrates component events.The window contains a panel that has a label and a check box.The check box controls whether the label is visible.A text area displays a message every timethe window, panel, label, or check boxfires a component event.<p><p><center><IMG SRC="../../figures/uiswing/events/ComponentEventDemo.png" WIDTH="334" HEIGHT="235" 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/ComponentEventDemo.jnlp">Run ComponentEventDemo</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#ComponentEventDemo">example index</a>.<li> When the window appears, one or more component-shown events have been fired.<li> Click the check box to hide the label. <br> The label fires a component-hidden event. The panel fires component-moved and component-resized events. The check box fires a component-moved event.<li> Click the check box again to show the label. <br> The label fires a component-shown event. The panel fires component-moved and component-resized events. The check box fires a component-moved event.<li> Iconify and then deiconify the window. <br> You do <em>not</em> get component-hidden or -shown events. If you want to be notified of iconification events, you should use a window listener or a window state listener.<li> Resize the window. <br> You'll see component-resized (and possibly component-moved) events from all four components — label, check box, panel, and frame. If the frame and panel's layout manager didn't make every component as wide as possible, the panel, label, and check box wouldn't have been resized.</ol><hr></blockquote><p>You can find the demo's code in <a class="SourceLink" target="_blank" href="examples/ComponentEventDemo.java"><code>ComponentEventDemo.java</code></a>.Here is just the code related to handling component events:<blockquote><pre>public class ComponentEventDemo ... implements ComponentListener { static JFrame frame; JLabel label; ... public ComponentEventDemo() { ... JPanel panel = new JPanel(new BorderLayout()); label = new JLabel("This is a label", JLabel.CENTER); label.addComponentListener(this); panel.add(label, BorderLayout.CENTER); JCheckBox checkbox = new JCheckBox("Label visible", true); checkbox.addComponentListener(this); panel.add(checkbox, BorderLayout.PAGE_END); panel.addComponentListener(this); ... frame.addComponentListener(this); } ... public void componentHidden(ComponentEvent e) { displayMessage("componentHidden event from " + e.getComponent().getClass().getName()); } public void componentMoved(ComponentEvent e) { Component c = e.getComponent(); displayMessage("componentMoved event from " + c.getClass().getName() + "; new location: " + c.getLocation().x + ", " + c.getLocation().y); } public void componentResized(ComponentEvent e) { Component c = e.getComponent(); displayMessage("componentResized event from " + c.getClass().getName() + "; new size: " + c.getSize().width + ", " + c.getSize().height); } public void componentShown(ComponentEvent e) { displayMessage("componentShown event from " + e.getComponent().getClass().getName()); } public static void main(String[] args) { ... //Create and set up the window. frame = new JFrame("ComponentEventDemo"); ... JComponent newContentPane = new ComponentEventDemo(); frame.setContentPane(newContentPane); ... }}</pre></blockquote></blockquote><h3><a name="api">The Component Listener API</a></h3><blockquote><p align=center><a name="componentlistener">The ComponentListener Interface</a><p><em>All of these methods are also in the adapter class,<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/ComponentAdapter.html"><code>ComponentAdapter</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/ComponentListener.html#componentHidden(java.awt.event.ComponentEvent)">componentHidden(ComponentEvent)</a></td><td> Called after the listened-to component is hidden as the result of the <code>setVisible</code> method being called.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/ComponentListener.html#componentMoved(java.awt.event.ComponentEvent)">componentMoved(ComponentEvent)</a></td><td> Called after the listened-to component moves, relative to its container. For example, if a window is moved, the window fires a component-moved event, but the components it contains do not.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/ComponentListener.html#componentResized(java.awt.event.ComponentEvent)">componentResized(ComponentEvent)</a></td><td> Called after the listened-to component's size (rectangular bounds) changes.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/ComponentListener.html#componentShown(java.awt.event.ComponentEvent)">componentShown(ComponentEvent)</a></td><td> Called after the listened-to component becomes visible as the result of the <code>setVisible</code> method being called.</td></tr></table><p align=center><a name="componentevent">The ComponentEvent 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/ComponentEvent.html#getComponent()">Component getComponent()</a></td><td> Returns the component that fired the event. You can use this instead of the <code>getSource</code> method.</td></tr></table></blockquote><a name="eg"><h3>Examples that Use Component Listeners</h3></a><blockquote>The following table lists theexamples that use component 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#ComponentEventDemo"><code>ComponentEventDemo</code></a></td><td> This section</td><td> Reports all component events that occur on several components to demonstrate the circumstances under which component events are fired.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=changelistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=containerlistener.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 Change Listener <br><b>Next page:</b> How to Write a Container Listener </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?