action.html

来自「jsf、swing的官方指南」· HTML 代码 · 共 773 行 · 第 1/2 页

HTML
773
字号
<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractAction.html"><code>AbstractAction</code></a> and then instantiate it.In your subclass, you must implement the <code>actionPerformed</code> method to react appropriately when the action event occurs.Here's an example of creating and instantiatingan <code>AbstractAction</code> subclass:<blockquote><pre>leftAction = new LeftAction("Go left", anIcon,             "This is the left button.",             new Integer(KeyEvent.VK_L));...class LeftAction extends AbstractAction {    public LeftAction(String text, ImageIcon icon,                      String desc, Integer mnemonic) {        super(text, icon);        putValue(SHORT_DESCRIPTION, desc);        putValue(MNEMONIC_KEY, mnemonic);    }    public void actionPerformed(ActionEvent e) {        displayResult("Action for first button/menu item", e);    }}</pre></blockquote><p>When the action created by the preceding code is attached to abutton and a menu item, the button and menu item display the text and icon associated with the action.  The <code>L</code>character is used for mnemonics on the button and menu item, andtheir tool-tip text is set to the <code>SHORT_DESCRIPTION</code>string followed by a representation of the mnemonic key.<p>For example, we have provided a simple example,<a class="SourceLink" target="_blank" href="examples/ActionDemo.java"><code>ActionDemo.java</code></a>, which defines three actions. Each action is attached to a buttonand a menu item.Thanks to the mnemonic values set for each button's action, the keysequence <code>Alt-L</code> activates the left button, <code>Alt-M</code>the middle button, and <code>Alt-R</code> the right button.  The tool tipfor the left button displays <em>This is the left button. Alt-L.</em>All of this configuration occurs automatically, without the program makingexplicit calls to set the mnemonic or tool-tip text.  As we'll showlater, the program <em>does</em> make calls to set the button text, but only to avoid using the values already set by the actions.<p><p><center><IMG SRC="../../figures/uiswing/misc/ActionDemo.png" WIDTH="458" HEIGHT="207" ALIGN="BOTTOM" ALT="A snapshot of ActionDemo, which uses actions to coordinate menus and buttons."></center></p><blockquote><hr><strong>Try this:</strong>&nbsp;<ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/misc/examples/ActionDemo.jnlp">Run ActionDemo</a> using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">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#ActionDemo">example index</a>.<li> Choose the top item from the left menu (<b>Menu &gt; Go left</b>).     <br>     The text area displays some text identifying both     the event source and     the action listener that received the event.<li> Click the leftmost button in the tool bar.     <br>     The text area again displays      information about the event.     Note that although the source of the events is different,     both events were detected by the same action listener:     the <code>Action</code> object attached to the components.<li> Choose the top item from the <b>Action State</b> menu.     <br>     This disables the "Go left" <code>Action</code> object,     which in turn disables     its associated menu item and button.</ol><hr></blockquote>Here is what the user seeswhen the "Go left" action is disabled:<table border="0"><tr><td><p><center><IMG SRC="../../figures/uiswing/misc/ActionDemo-a.png" WIDTH="458" HEIGHT="207" ALIGN="BOTTOM" ALT="A snapshot of ActionDemo when "Go Left" is disabled."></center></p></td><td><p><center><IMG SRC="../../figures/uiswing/misc/ActionDemo-b.png" WIDTH="458" HEIGHT="207" ALIGN="BOTTOM" ALT="A snapshot of ActionDemo when "Go Left" is disabled."></center></p></td></tr></table><p>Here's the code that disables the "Go left" action:<blockquote><pre>boolean selected = ...//<em>true if the action should be enabled;</em>                      //<em>false, otherwise</em>leftAction.setEnabled(selected);</pre></blockquote>After you create components using an <code>Action</code>,you might well need to customize them.For example, you might want to customize theappearance of one of the componentsby adding or deleting the icon or text.For example, <a class="SourceLink" target="_blank" href="examples/ActionDemo.java"><code>ActionDemo.java</code></a>has no icons in its menus, and no text in its buttons.Here's the code that accomplishes this:<blockquote><pre>menuItem = new JMenuItem();menuItem.setAction(leftAction);menuItem.setIcon(null); //arbitrarily chose not to use icon in menu...button = new JButton();button.setAction(leftAction);button.setText(""); //an icon-only button</pre></blockquote><p>We chose to create an icon-only button anda text-only menu item from the same action by settingthe icon property to <code>null</code> and the textto an empty string.However, if a property of the <code>Action</code> changes, thewidget may try to reset the icon and text from the<code>Action</code> again.</blockquote><h3><a name="api">The Action API</a></h3><blockquote>The following tables list the commonly used<code>Action</code> constructors and methods.The API for using <code>Action</code> objectsfalls into three categories:<ul><li><a href="#actioncomponents">Components that Support set/getAction</a><li><a href="#actionapi">Creating and Using an AbstractAction</a><li><a href="#properties">Action Properties</a></ul><p><table border=1><caption><a name="actioncomponents">Components that Support set/getAction</a></caption><tr><th align=left>Class</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/AbstractButton.html#setAction(javax.swing.Action)">AbstractButton</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#setAction(javax.swing.Action)">JComboBox</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#setAction(javax.swing.Action)">JTextField</a></td><td>As of release 1.3, these components and theirsubclasses may have an action directly assignedto them via <code>setAction</code>.For further information about components that are oftenassociated with actions, see the sections on<a class="TutorialLink" target="_top" href="../components/toolbar.html">tool bar buttons</a>,<a class="TutorialLink" target="_top" href="../components/menu.html">menu items</a>,     <a class="TutorialLink" target="_top" href="../components/button.html">common buttons</a>,    and<a class="TutorialLink" target="_top" href="../components/textfield.html">text fields</a>.For details on which propertieseach component takes from the <code>Action</code>,see the API documentation for the relevant class's<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html#configurePropertiesFromAction(javax.swing.Action)"><code>configurePropertiesFromAction</code></a> method.</tr></tr></table><p><table border=1> <caption><a name="actionapi">Creating and Using an AbstractAction</a></caption><tr><th align=left>Constructor or 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/AbstractAction.html#AbstractAction()">AbstractAction()</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractAction.html#AbstractAction(java.lang.String)">AbstractAction(String)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractAction.html#AbstractAction(java.lang.String, javax.swing.Icon)">AbstractAction(String, Icon)</a></td><td>Create an <code>Action</code> object.    Through arguments,    you can specify the text and icon    to be used in the components that the action controls.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractAction.html#setEnabled(boolean)">void setEnabled(boolean)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractAction.html#isEnabled()">boolean isEnabled()</a></td><td>Set or get whether the components the action controls are enabled.    Invoking <code>setEnabled(false)</code>    disables all the components that the action controls.    Similarly, invoking <code>setEnabled(true)</code>    enables the action's components.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractAction.html#putValue(java.lang.String, java.lang.Object)">void putValue(String, Object)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractAction.html#getValue(java.lang.String)">Object getValue(String)</a></td><td>Set or get an object associated with a specified key.    Used for setting and getting properties associated    with an action.</td></tr></table><p><p align=center><a name="properties">Action Properties</a><p>This table defines the properties that can be set on anaction.  The second column lists which components automaticallyuse the properties (and what method is specifically called).For example,setting the <code>ACCELERATOR_KEY</code> on an action thatis then attached to a menu item, means that<code>JMenuItem.setAccelerator(KeyStroke)</code>is called automatically.<p><table border=1> <tr><th align=left>Property</th><th align=left>Auto-Applied to: <br>Class <br><em>(Method Called)</em></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/Action.html#ACCELERATOR_KEY">ACCELERATOR_KEY</a></td><td><code>JMenuItem</code>    <br>(<em>setAccelerator</em>)</td><td>The <code>KeyStroke</code> to be used as the accelerator for    the action.    For a discussion of accelerators versus mnemonics, see<a class="TutorialLink" target="_top" href="../components/menu.html#mnemonic">Enabling Keyboard Operation.</a>    Introduced in 1.3.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Action.html#ACTION_COMMAND_KEY">ACTION_COMMAND_KEY</a></td><td><code>AbstractButton</code>, <code>JCheckBox</code>,    <code>JRadioButton</code>    <br>(<em>setActionCommand</em>)</td><td>The command string associated with the    <code>ActionEvent</code>.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Action.html#LONG_DESCRIPTION">LONG_DESCRIPTION</a></td><td>None</td><td>The longer description for the action.    Can be used for context-sensitive help.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Action.html#MNEMONIC_KEY">MNEMONIC_KEY</a></td><td><code>AbstractButton</code>, <code>JMenuItem</code>,    <code>JCheckBox</code>, <code>JRadioButton</code>    <br>(<em>setMnemonic</em>)</td><td>The mnemonic for the action.    For a discussion of accelerators versus mnemonics, see<a class="TutorialLink" target="_top" href="../components/menu.html#mnemonic">Enabling Keyboard Operation.</a>    Introduced in 1.3. </td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Action.html#NAME">NAME</a></td><td><code>AbstractButton</code>, <code>JMenuItem</code>,    <code>JCheckBox</code>, <code>JRadioButton</code>    <br>(<em>setText</em>)</td><td>The name of the action.    You can set this property when creating the action using    the <code>AbstractAction(String)</code> or     <code>AbstractAction(String, Icon)</code> constructors.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Action.html#SHORT_DESCRIPTION">SHORT_DESCRIPTION</a></td><td><code>AbstractButton</code>,    <code>JCheckBox</code>, <code>JRadioButton</code>    <br>(<em>setToolTipText</em>)</td><td>The short description of the action.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Action.html#SMALL_ICON">SMALL_ICON</a></td><td><code>AbstractButton</code>, <code>JMenuItem</code>    <br>(<em>setIcon</em>)</td><td>The icon for the action used in the tool bar or on    a button.    You can set this property when creating the action using    the <code>AbstractAction(name, icon)</code> constructor.</td></tr></table></blockquote><h3><a name="eg">Examples that Use Actions</a></h3><blockquote>The following examplesuse <code>Action</code> objects.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td> <a href="examples/index.html#ActionDemo"><code>ActionDemo</code></a></td><td> This section</td><td> Uses actions to bind buttons and menu items to the same function.</td></tr><tr><td> <a href="../components/examples/index.html#TextComponentDemo">     <code>TextComponentDemo</code></a></td><td> <a class="TutorialLink" target="_top" href="../components/generaltext.html">Text Component Features</a></td><td> Uses text actions to create menu items     for text editing commands, such as cut, copy, and paste,     and to bind key strokes to caret movement.     Also implements custom <code>AbstractAction</code> subclasses     to implement undo and redo.     The text action discussion begins in     <a href="../components/generaltext.html#editorkits">Concepts:     About Editor Kits</a>.</td></tr></table>        </blockquote>        <div class=NavBit>            <a target=_top href=index.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=timer.html>Next &raquo;</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> Using Other Swing Features        <br><b>Next page:</b> How to Use Swing Timers    </div>    </body></html> 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?