The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
An Action
can be used to separate functionality and state from a component. Action
可用于从组件中分离功能和状态。For example, if you have two or more components that perform the same function, consider using an 例如,如果有两个或多个组件执行相同的功能,请考虑使用Action
object to implement the function. Action
对象来实现该功能。An Action
object is an action listener that provides not only action-event handling, but also centralized handling of the state of action-event-firing components such as tool bar buttons, menu items, common buttons, and text fields. Action
对象是一个动作监听器,它不仅提供动作事件处理,还提供对动作事件触发组件(如工具栏按钮、菜单项、公共按钮和文本字段)状态的集中处理。The state that an action can handle includes text, icon, mnemonic, enabled, and selected status.操作可以处理的状态包括文本、图标、助记符、启用和选定状态。
You typically attach an action to a component using the 通常使用setAction
method. setAction
方法将动作附加到组件。Here's what happens when 下面是在组件上调用setAction
is invoked on a component:setAction
时发生的情况:
Action
. Action
的状态。Action
's text and icon values were set, the component's text and icon are set to those values.Action
的文本和图标值,则组件的文本和按钮将设置为这些值。Action
object is registered as an action listener on the component.Action
对象注册为组件上的操作侦听器。Action
changes, the component's state is updated to match the Action
. Action
的状态更改,组件的状态将更新为与Action
匹配。Here's an example of creating a tool-bar button and menu item that perform the same function:以下是创建执行相同功能的工具栏按钮和菜单项的示例:
Action leftAction = new LeftAction(); //LeftAction code is shown later ... button = new JButton(leftAction) ... menuItem = new JMenuItem(leftAction);
To create an 要创建Action
object, you generally create a subclass of AbstractAction
and then instantiate it. Action
对象,通常创建AbstractAction
的子类,然后实例化它。In your subclass, you must implement the 在子类中,必须实现actionPerformed
method to react appropriately when the action event occurs. actionPerformed
方法,以便在操作事件发生时做出适当的反应。Here's an example of creating and instantiating an 下面是创建和实例化AbstractAction
subclass:AbstractAction
子类的示例:
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); } }
When the action created by the preceding code is attached to a button and a menu item, the button and menu item display the text and icon associated with the action. 当前面代码创建的动作附加到按钮和菜单项时,按钮和菜单项目将显示与动作关联的文本和图标。The L
character is used for mnemonics on the button and menu item, and their tool-tip text is set to the SHORT_DESCRIPTION
string followed by a representation of the mnemonic key.L
字符用于按钮和菜单项上的助记符,其工具提示文本设置为SHORT_DESCRIPTION
字符串,后跟助记符键的表示。
For example, we have provided a simple example, 例如,我们提供了一个简单的例子ActionDemo.java
, which defines three actions. ActionDemo.java
,它定义了三个动作。Each action is attached to a button and a menu item. 每个动作都附加到一个按钮和一个菜单项。Thanks to the mnemonic values set for each button's action, the key sequence 由于为每个按钮的动作设置了助记符值,按键序列Alt-L激活左按钮,Alt-M激活中间按钮,Alt-R激活右按钮。Alt-L
activates the left button, Alt-M
the middle button, and Alt-R
the right button. The tool tip for the left button displays This is the left button. Alt-L. 左侧按钮的工具提示显示“这是左侧按钮”。Alt-L。All of this configuration occurs automatically, without the program making explicit calls to set the mnemonic or tool-tip text. 所有这些配置都会自动发生,而程序无需显式调用来设置助记符或工具提示文本。As we'll show later, the program does make calls to set the button text, but only to avoid using the values already set by the actions.正如我们稍后将要展示的,程序确实会调用来设置按钮文本,但只是为了避免使用操作已经设置的值。
Click the Launch button to run ActionDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行ActionDemo(下载JDK 7或更高版本)。Or, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
Choose the top item from the left menu (Menu > Go left).从左侧菜单中选择顶部项目(菜单>向左移动)。
The text area displays some text identifying both the event source and the action listener that received the event.文本区域显示一些文本,标识事件源和接收事件的动作监听器。
Click the leftmost button in the tool bar.单击工具栏中最左侧的按钮。
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 请注意,尽管事件的来源不同,但这两个事件都是由同一个动作监听器检测到的:附加到组件的Action
object attached to the components.Action
对象。
Choose the top item from the Action State menu.从“操作状态”菜单中选择顶部项目。
This disables the "Go left" 这将禁用“Go left”Action
object, which in turn disables its associated menu item and button.Action
对象,进而禁用其关联菜单项和按钮。
Here is what the user sees when the "Go left" action is disabled:以下是用户在禁用“向左”操作时看到的内容:
![]() |
![]() |
Here's the code that disables the "Go left" action:下面是禁用“向左走”操作的代码:
boolean selected = ...//true if the action should be enabled; //false, otherwise leftAction.setEnabled(selected);
After you create components using an 使用Action
, you might well need to customize them. Action
创建组件后,您可能需要对其进行自定义。For example, you might want to customize the appearance of one of the components by adding or deleting the icon or text. 例如,您可能希望通过添加或删除图标或文本来自定义某个组件的外观。For example, 例如,ActionDemo.java
has no icons in its menus, and no text in its buttons. Here's the code that accomplishes this:ActionDemo.java
的菜单中没有图标,按钮中没有文本。下面是实现这一点的代码:
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
We chose to create an icon-only button and a text-only menu item from the same action by setting the icon property to 我们选择通过将图标属性设置为null
and the text to an empty string. null
,将文本设置为空字符串,从同一操作中创建仅图标按钮和仅文本菜单项。However, if a property of the 但是,如果Action
changes, the widget may try to reset the icon and text from the Action
again.Action
的属性发生更改,小部件可能会再次尝试重置Action
中的图标和文本。
The following tables list the commonly used 下表列出了常用的Action
constructors and methods. Action
构造函数和方法。The API for using 使用Action
objects falls into three categories:Action
对象的API分为三类:
AbstractAction
Action
AbstractButton JComboBox JTextField |
setAction . setAction 直接分配给它们的动作。Action , see the API documentation for the relevant class's configurePropertiesFromAction method. Action 获取哪些属性的详细信息,请参阅相关类的configurePropertiesFromAction 方法的API文档。buttonActions table.buttonActions 表。 |
AbstractAction() AbstractAction(String) AbstractAction(String, Icon) |
Action object. Action 对象。 |
void setEnabled(boolean) boolean isEnabled() |
setEnabled(false) disables all the components that the action controls. setEnabled(false) 将禁用操作控制的所有组件。setEnabled(true) enables the action's components.setEnabled(true) 将启用操作的组件。 |
void putValue(String, Object) Object getValue(String) |
This table defines the properties that can be set on an action. 此表定义了可以在操作上设置的属性。The second column lists which components automatically use the properties (and what method is specifically called). 第二列列出了哪些组件自动使用属性(以及具体调用的方法)。For example, setting the 例如,在随后附加到菜单项的操作上设置ACCELERATOR_KEY
on an action that is then attached to a menu item, means that JMenuItem.setAccelerator(KeyStroke)
is called automatically.ACCELERATOR_KEY
,意味着JMenuItem.setAccelerator(KeyStroke)
将自动调用。
ACCELERATOR_KEY |
JMenuItem (setAccelerator) |
KeyStroke to be used as the accelerator for the action. KeyStroke 。 |
ACTION_COMMAND_KEY |
AbstractButton , JCheckBox , JRadioButton (setActionCommand) |
ActionEvent .ActionEvent 关联的命令字符串。 |
LONG_DESCRIPTION |
None | |
MNEMONIC_KEY |
AbstractButton , JMenuItem , JCheckBox , JRadioButton (setMnemonic) |
|
NAME |
AbstractButton , JMenuItem , JCheckBox , JRadioButton (setText) |
AbstractAction(String) or AbstractAction(String, Icon) constructors.AbstractAction(String) 或AbstractAction(Striing, Icon) 构造函数创建操作时设置此属性。 |
SHORT_DESCRIPTION |
AbstractButton , JCheckBox , JRadioButton (setToolTipText) |
|
SMALL_ICON |
AbstractButton , JMenuItem (setIcon) |
AbstractAction(name, icon) constructor.AbstractAction(name, icon) 构造函数创建操作时设置此属性。 |
The following examples use 以下示例使用Action
objects.Action
对象。
ActionDemo |
||
TextComponentDemo |
AbstractAction subclasses to implement undo and redo. AbstractAction 子类以实现撤消和重做。 |