intro.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 547 行 · 第 1/2 页
HTML
547 行
<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 </div> <div id=LeftBar class=LeftBar_shown> <div id=Contents> <div class="linkLESSON"><a href="index.html">Writing Event Listeners</a></div><div class="nolinkAHEAD">Introduction to Event Listeners</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="linkBHEAD"><a href="containerlistener.html">How to Write a Container Listener</a></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=index.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=generalrules.html>Next »</a> </div> <div id=PageTitle>Introduction to Event Listeners</div> <blockquote>If you've read<a class="TutorialLink" target="_top" href="../learn/example2.html#handlingEvents">Handling Events</a> or any of thecomponent how-to pages,you probably already know the basics of event listeners.<p>Let's look at one of the simplest event handlingexamples possible. It's called Beeper, and it features a buttonthat beeps when you click it. You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/events/examples/Beeper.jnlp">run Beeper</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#Beeper">example index</a>.<p><p><center><IMG SRC="../../figures/uiswing/events/Beeper.png" WIDTH="200" HEIGHT="80" ALIGN="BOTTOM" ALT=""></center></p>You can find the entire program in<a class="SourceLink" target="_blank" href="examples/Beeper.java"><code>Beeper.java</code></a>.Here's the code that implements the event handlingfor the button:<blockquote><pre>public class Beeper ... implements ActionListener { ... <em>//where initialization occurs:</em> button.addActionListener(this); ... public void actionPerformed(ActionEvent e) { <em>...//Make a beep sound...</em> }}</pre></blockquote>The <code>Beeper</code> class implements the <a href="actionlistener.html"><code>ActionListener</code></a>interface, which contains one method:<code>actionPerformed</code>.Since <code>Beeper</code> implements <code>ActionListener</code>,a <code>Beeper</code> object can register as a listenerfor the action events that buttons fire.Once the <code>Beeper</code> has been registeredusing the <code>Button</code> <code>addActionListener</code> method,the <code>Beeper</code>'s <code>actionPerformed</code> methodis called every time the button is clicked.</blockquote><h3><a name="example2">A More Complex Example</a></h3><blockquote>The event model,which you saw at its simplest in the preceding example,is quite powerful and flexible.Any number of event listener objectscan listen for all kinds of eventsfrom any number of event source objects.For example, a programmight create one listener per event source.Or a program might have a single listenerfor all events from all sources.A program can even have more than one listenerfor a single kind of event from a single event source.<p><center><IMG SRC="../../figures/uiswing/events/2eventsource.gif" WIDTH="365" HEIGHT="85" ALIGN="BOTTOM" ALT="Event source with multiple listeners"></center></p><h4 align=center>Multiple listenerscan register to be notified of eventsof a particular type from a particular source. Also, the same listener can listen to notifications fromdifferent objects.<font color=red>[PENDING: Update this diagram withan additional event source that also sends notification tothe top-most event listener.]</font></h4><p>Each event is represented by an object that givesinformation about the event and identifies the eventsource. Event sources are often components or models,but other kinds of objects can also be event sources.<p>Whenever you want to detect events from a particular component,first check the how-to section for that component. A list of thecomponent how-to sections is <a class="TutorialLink" target="_top" href="../components/componentlist.html">here</a>.The how-to sections give examples of handling theevents that you're most likely to care about. In<a class="TutorialLink" target="_top" href="../components/colorchooser.html">How to Use Color Choosers</a>, for instance, you'll find an exampleof writing a change listener to track when the colorchanges in the color chooser.<p>The following example demonstrates that event listenerscan be registered on multiple objects and thatthe same event can be sent to multiple listeners.The example contains two event sources (<code>JButton</code> instances)and two event listeners.One of the event listeners(an instance of a class called <code>MultiListener</code>)listens for events from both buttons.When it receives an event, it adds the event's "action command"(which is set to the text on the button's label)to the top text area.The second event listener(an instance of a class called <code>Eavesdropper</code>)listens for events on only one of the buttons.When it receives an event,it adds the action command to the bottom text area.<p><p><center><IMG SRC="../../figures/uiswing/events/MultiListener.gif" WIDTH="400" HEIGHT="300" ALIGN="BOTTOM" ALT=""></center></p><h4 align=center><font color=red>[PENDING: Updated screenshot forthcoming.]</font></h4><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/events/examples/MultiListener.jnlp">Run MultiListener</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#MultiListener">example index</a>.<li> Click the <em>Blah blah blah</em> button. Only the <code>MultiListener</code> object is registered to listen to this button.<li> Click the <em>You don't say!</em> button. Both the <code>MultiListener</code> object and the <code>Eavesdropper</code> object are registered to listen to this button.<hr></blockquote>You can find the entire program in<a class="SourceLink" target="_blank" href="examples/MultiListener.java"><code>MultiListener.java</code></a>.Here's the code that implements the event handlingfor the button:<blockquote><pre>public class MultiListener ... implements ActionListener { ... <em>//where initialization occurs:</em> button1.addActionListener(this); button2.addActionListener(this); button2.addActionListener(new Eavesdropper(bottomTextArea)); } public void actionPerformed(ActionEvent e) { topTextArea.append(e.getActionCommand() + newline); }}class Eavesdropper implements ActionListener { ... public void actionPerformed(ActionEvent e) { myTextArea.append(e.getActionCommand() + newline); }}</pre></blockquote>In the above code, both <code>MultiListener</code> and <code>Eavesdropper</code>implement the <code>ActionListener</code> interfaceand register as action listeners using the <code>JButton</code> <code>addActionListener</code> method.Both classes' implementations of the <code>actionPerformed</code> methodare similar:they simply add the event's action command to a text area. </blockquote> </blockquote> <div class=NavBit> <a target=_top href=index.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=generalrules.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> Writing Event Listeners <br><b>Next page:</b> General Information about Writing Event Listeners </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?