actionlistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 608 行 · 第 1/2 页
HTML
608 行
<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=handling.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=caretlistener.html>Next »</a> </div> <div id=PageTitle>How to Write an Action Listener</div> <blockquote>Action listeners are probably the easiest — and mostcommon — event handlers to implement.You implement an action listenerto respond to the user's indicationthat some implementation-dependent action should occur.<p>When the user clicks a <a class="TutorialLink" target="_top" href="../components/button.html">button</a>, chooses a <a class="TutorialLink" target="_top" href="../components/menu.html">menu item</a> or presses Enter in a <a class="TutorialLink" target="_top" href="../components/textfield.html">text field</a>, an action event occurs.The result is that an <code>actionPerformed</code> message is sent to all action listeners that are registered on the relevant component.<p>Here is the action event handling code from an applet named<code>Beeper</code>:<blockquote><pre>public class Beeper ... implements ActionListener { ... <em>//where initialization occurs:</em> button.addActionListener(this); ... public void actionPerformed(ActionEvent e) { Toolkit.getDefaultToolkit().beep(); }}</pre></blockquote>The <code>Beeper</code> applet is describedin this trail's introduction to events,<a href="intro.html">Introduction to Event Listeners</a>.You can find the entire program in <a class="SourceLink" target="_blank" href="examples/Beeper.java"><code>Beeper.java</code></a>.The other example described in that section,<a class="SourceLink" target="_blank" href="examples/MultiListener.java"><code>MultiListener.java</code></a>,has two action sources and two action listeners,with one listener listening to both sourcesand the other listening to just one.</blockquote><h3><a name="api">The Action Listener API</a></h3><blockquote><p align=center><a name="actionlistener">The ActionListener Interface</a><p><em>Because <code>ActionListener</code> has only one method, it has no corresponding adapter class.</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/ActionListener.html#actionPerformed(java.awt.event.ActionEvent)">actionPerformed(actionEvent)</a></td><td> Called just after the user informs the listened-to component that an action should occur.</td></tr></table><p align=center><a name="actionevent">The ActionEvent 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/ActionEvent.html#getActionCommand()">String getActionCommand()</a></td><td>Returns the string associated with this action. Most objects that can fire action events support a method called <code>setActionCommand</code> that lets you set this string.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/ActionEvent.html#getModifiers()">int getModifiers()</a></td><td>Returns an integer representing the modifier keys the user was pressing when the action event occurred. You can use the <code>ActionEvent</code>-defined constants <code>SHIFT_MASK</code>, <code>CTRL_MASK</code>, <code>META_MASK</code>, and <code>ALT_MASK</code> to determine which keys were pressed. For example, if the user Shift-selects a menu item, then the following expression is nonzero: <blockquote><pre>actionEvent.getModifiers() & ActionEvent.SHIFT_MASK </pre></blockquote></td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/util/EventObject.html#getSource()">Object getSource()</a><br>(<em>in <code>java.util.EventObject</code></em>)</td><td>Returns the object that fired the event.</td></tr></table></blockquote><h3><a name="eg">Examples that Use Action Listeners</a></h3><blockquote>The following table lists some of the manyexamples that use action 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#Beeper"><code>Beeper</code></a></td><td> This section and <a href="intro.html">Introduction to Event Listeners</a></td><td> Contains one button with one action listener that beeps when you click the button.</td></tr><tr valign=top><td> <a href="examples/index.html#MultiListener"><code>MultiListener</code></a></td><td> <a href="intro.html">Introduction to Event Listeners</a></td><td> Registers two different action listeners on one button. Also registers the same action listener on two different buttons.</td></tr><tr valign=top><td> <a href="../components/examples/index.html#RadioButtonDemo"><code>RadioButtonDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/button.html#radiobutton">How to Use Radio Buttons</a></td><td> Registers the same action listener on five radio buttons. The listener uses the <code>getActionCommand</code> method to determine which radio button fired the event.</td></tr><tr valign=top><td> <a href="../components/examples/index.html#MenuDemo"><code>MenuDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/menu.html">How to Use Menus</a></td><td> Shows how to listen for action events on menu items.</td></tr><tr valign=top><td> <a href="../misc/examples/index.html#DragPictureDemo2"><code>DragPictureDemo2</code></a></td><td><a class="TutorialLink" target="_top" href="../dnd/intro.html#cut">Adding Cut/Copy/Paste Support to Data Transfer</a></td><td>Uses <code>setActionCommand</code> to attach the cut, copy, and paste actions to the menu. Then uses as action listener to forward the cut/copy/paste actions to the currently focused component.</td></tr><tr valign=top><td> <a href="../components/examples/index.html#TextDemo"><code>TextDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/textfield.html">How to Use Text Fields</a></td><td> An applet that registers an action listener on a text field.</td></tr><tr valign=top><td> <a href="../components/examples/index.html#IconDemoApplet"><code>IconDemoApplet</code></a></td><td><a class="TutorialLink" target="_top" href="../components/icon.html">How to Use Icons</a></td><td> Loads an image in an action listener. Because loading an image can take a while, this program uses a <code>SwingWorker</code> to load the image in a background thread.</td></tr><tr valign=top><td> <a href="../components/examples/index.html#TableDialogEditDemo"><code>TableDialogEditDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/table.html">How to Use Tables</a></td><td> Registers an action listener through a factory method on the OK button of a color chooser dialog.</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> Registers an action listener on a timer that controls an animation loop.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=handling.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=caretlistener.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> Implementing Listeners for Commonly Handled Events <br><b>Next page:</b> How to Write a Caret Listener </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?