public interface Action extends ActionListener
Action接口提供了有用的扩展, ActionListener在箱子接口,其中相同的功能可以由若干控件访问。
除了actionPerformed由定义的方法ActionListener接口,该接口允许定义,在一个地方的应用:
此接口可以添加到现有类或用于创建适配器(通常通过子类化AbstractAction )。 然后可以将Action对象添加到多个Action -aware容器中,并连接到Action的可组件。 然后可以通过调用Action对象的setEnabled方法一次性激活或禁用GUI控件。
请注意, Action实现在存储方面倾向于比典型的ActionListener ,这不具有集中控制功能和广播属性更改的优点。 出于这个原因,你应该注意只使用Action哪里需要这些优点的S,并且使用简单ActionListener别处秒。
Action 很多Swing的组件都有一个Action属性。 当Action上设置Action时,会发生以下情况:
Action作为ActionListener的ActionListener添加。 Action 。 PropertyChangeListener上Action ,使得部件可以改变其性能以反映变化Action的属性。 下表描述了由所使用的属性Swing支持部件Actions 。 在表中, 按钮指的是任何AbstractButton子类,其中不仅包括JButton ,还包括类,如JMenuItem 。 除非另有说明, Action (或Action ,即null )中的null属性值导致按钮的相应属性设置为null 。
enabled All The isEnabled method toolTipText All SHORT_DESCRIPTION actionCommand All ACTION_COMMAND_KEY mnemonic All buttons MNEMONIC_KEY A null value or Action results in the button's mnemonic property being set to '\0'. text All buttons NAME If you do not want the text of the button to mirror that of the Action, set the property hideActionText to true. If hideActionText is true, setting the Action changes the text of the button to null and any changes to NAME are ignored. hideActionText is useful for tool bar buttons that typically only show an Icon. JToolBar.add(Action) sets the property to true if the Action has a non-null value for LARGE_ICON_KEY or SMALL_ICON. displayedMnemonicIndex All buttons DISPLAYED_MNEMONIC_INDEX_KEY If the value of DISPLAYED_MNEMONIC_INDEX_KEY is beyond the bounds of the text, it is ignored. When setAction is called, if the value from the Action is null, the displayed mnemonic index is not updated. In any subsequent changes to DISPLAYED_MNEMONIC_INDEX_KEY, null is treated as -1. icon All buttons except of JCheckBox, JToggleButton and JRadioButton. either LARGE_ICON_KEY or SMALL_ICON The JMenuItem subclasses only use SMALL_ICON. All other buttons will use LARGE_ICON_KEY; if the value is null they use SMALL_ICON. accelerator All JMenuItem subclasses, with the exception of JMenu. ACCELERATOR_KEY selected JToggleButton, JCheckBox, JRadioButton, JCheckBoxMenuItem and JRadioButtonMenuItem SELECTED_KEY Components that honor this property only use the value if it is non-null. For example, if you set an Action that has a null value for SELECTED_KEY on a JToggleButton, the JToggleButton will not update it's selected state in any way. Similarly, any time the JToggleButton's selected state changes it will only set the value back on the Action if the Action has a non-null value for SELECTED_KEY. Action is used with multiple components, all the components keep their selected state in sync with this property. Mutually exclusive buttons, such as JToggleButtons in a ButtonGroup, force only one of the buttons to be selected. As such, do not use the same Action that defines a value for the SELECTED_KEY property with multiple mutually exclusive buttons.
JPopupMenu , JToolBar和JMenu都提供了创建组件和设置方便的方法Action相应部件上。 有关更多信息,请参阅这些课程。
Action使用PropertyChangeListener通知听众Action已更改。 bean规范表示null属性名称可用于指示多个值已更改。 默认情况下,采用Action Swing组件不会处理此类更改。 为了表示Swing应该根据bean规范处理null ,系统属性swing.actions.reconfigureOnNull为String值为true 。
AbstractAction
| Modifier and Type | Field and Description |
|---|---|
static String |
ACCELERATOR_KEY
用于存储
KeyStroke作为加速器的动作的关键。
|
static String |
ACTION_COMMAND_KEY
用于确定该命令的键
String为
ActionEvent ,将当被创建
Action将被通知作为驻留在的结果
Keymap与相关联的
JComponent 。
|
static String |
DEFAULT
目前不使用
|
static String |
DISPLAYED_MNEMONIC_INDEX_KEY
用于存储的密钥
Integer对应于在文本(由所识别的索引
NAME属性),对于一个助记符的装饰应在被渲染。
|
static String |
LARGE_ICON_KEY
用于存储
Icon的键。
|
static String |
LONG_DESCRIPTION
用于存储更长的
String关键字可用于上下文相关帮助。
|
static String |
MNEMONIC_KEY
用于存储的密钥
Integer对应于所述的一个
KeyEvent键码。
|
static String |
NAME
用于存储用于操作的
String名称的键,用于菜单或按钮。
|
static String |
SELECTED_KEY
用于存储对应于所选状态的
Boolean的密钥。
|
static String |
SHORT_DESCRIPTION
用于存储用于操作的简短
String描述的键,用于工具提示文本。
|
static String |
SMALL_ICON
用于存储小钥匙的关键
Icon ,如
ImageIcon 。
|
| Modifier and Type | Method and Description |
|---|---|
void |
addPropertyChangeListener(PropertyChangeListener listener)
添加一个
PropertyChange监听器。
|
Object |
getValue(String key)
使用关联的键获取该对象的属性之一。
|
boolean |
isEnabled()
返回
Action的启用状态。
|
void |
putValue(String key, Object value)
使用关联的键设置此对象的属性之一。
|
void |
removePropertyChangeListener(PropertyChangeListener listener)
删除一个
PropertyChange监听器。
|
void |
setEnabled(boolean b)
设置
Action的启用状态。
|
actionPerformedstatic final String DEFAULT
static final String NAME
String名称的键,用于菜单或按钮。
static final String SHORT_DESCRIPTION
String描述的操作的键,用于工具提示文本。
static final String LONG_DESCRIPTION
String关键字可用于上下文相关帮助。
static final String SMALL_ICON
Icon ,如ImageIcon 。
这通常用于菜单,如JMenuItem 。
如果使用与菜单和按钮相同的Action则通常会同时指定SMALL_ICON和LARGE_ICON_KEY 。 菜单将使用SMALL_ICON ,按钮将使用LARGE_ICON_KEY 。
static final String ACTION_COMMAND_KEY
String为
ActionEvent ,将当被创建
Action将被通知作为驻留在的结果
Keymap与相关联的
JComponent 。
static final String ACCELERATOR_KEY
KeyStroke用作操作加速器的键。
static final String MNEMONIC_KEY
Integer对应于所述的一个KeyEvent键码。
该值通常用于指定助记符。
例如: myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)将myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)的助记符myAction为“a”,而myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar('?'))将助记符设置为myAction里文字myAction “Ef”的myAction 。
static final String SELECTED_KEY
Boolean的密钥。
这通常仅用于具有有意义的选择状态的组件。
例如, JRadioButton和JCheckBox使用这个但是JMenu的JMenu不。
该属性与其他属性不同,它们由组件读取并由组件设置。 例如,如果一个Action被附接到一个JCheckBox所述的选择状态JCheckBox将从所述的设置Action 。 如果用户点击JCheckBox的的选择状态JCheckBox 和 Action 都将被更新。
注意:该字段的值以“Swing”为前缀,以避免与现有的Actions 。
static final String DISPLAYED_MNEMONIC_INDEX_KEY
Integer对应于在文本(由所识别的索引NAME属性),对于一个助记符的装饰应在被渲染。
如果此属性的值大于或等于文本的长度,则将其视为-1。
注意:该字段的值以“Swing”为前缀,以避免与现有的Actions 。
AbstractButton.setDisplayedMnemonicIndex(int) ,
Constant Field Values
static final String LARGE_ICON_KEY
Icon 。
这通常由按钮使用,例如JButton和JToggleButton 。
如果Action与菜单和按钮一起使用,通常会同时指定一个SMALL_ICON和LARGE_ICON_KEY 。 菜单将使用SMALL_ICON和按钮LARGE_ICON_KEY 。
注意:该字段的值以“Swing”为前缀,以避免与现有的Actions 。
void putValue(String key, Object value)
PropertyChangeEvent向PropertyChangeEvent器发送PropertyChangeEvent。
key - 一个
String包含密钥
value - 一个
Object值
void setEnabled(boolean b)
Action的启用状态。
启用后,与此对象关联的任何组件都处于活动状态,并能够触发此对象的actionPerformed方法。
如果值已更改, PropertyChangeEvent向PropertyChangeEvent器发送PropertyChangeEvent。
b - true启用此
Action ,false禁用它
boolean isEnabled()
Action的启用状态。
启用后,与此对象关联的任何组件都将处于活动状态,并能够触发此对象的actionPerformed方法。
Action ,则为true
void addPropertyChangeListener(PropertyChangeListener listener)
PropertyChange监听器。
容器和附加组件使用这些方法注册对此Action对象的兴趣。
当其启用状态或其他属性发生变化时,会向注册的听众通知更改。
listener - 一个
PropertyChangeListener对象
void removePropertyChangeListener(PropertyChangeListener listener)
PropertyChange侦听器。
listener - 一个
PropertyChangeListener对象
addPropertyChangeListener(java.beans.PropertyChangeListener)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.