public interface Action extends ActionListener
Action
接口提供了一个有用的扩展,
ActionListener
接口的情况下,相同的功能可以由几个控件访问。
除了由ActionListener
接口中定义的actionPerformed
方法,该接口允许应用程序定义的,在一个地方:
该接口可以为已有的类添加或用于创建一个适配器(通常情况下,通过子类化AbstractAction
)。然后Action
对象可以被添加到多个Action
-aware容器和连接Action
-capable组件。GUI控件就可以激活或停用一次通过调用对象的方法Action
setEnabled
。
注意,Action
实现往往在存储方面是更昂贵的比一个典型的ActionListener
,它不提供对性能变化的功能和广播集中控制的好处。出于这个原因,你应该照顾他们的利益Action
s只使用需要,使用简单的ActionListener
s别处。
Action
许多Swing的组件有一个Action
财产。当一个Action
上设置的一个组成部分,以下的事情发生:
Action
作为ActionListener
到组件。Action
。Action
一PropertyChangeListener
使组件可以改变其属性反映在Action
性质发生改变。下表描述了由Swing
组件支持Actions
使用性能。在餐桌上,按钮是指任何AbstractButton
亚纲,其中不仅包括JButton
也类如JMenuItem
。除非另有说明,在Action
一null
属性值(或Action
,null
)在按钮的属性被设置为null
结果。
Component Property | Components | Action Key | Notes |
---|---|---|---|
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 . Components that honor this property keep their selected state in sync with this property. When the same Action is used with multiple components, all the components keep their selected state in sync with this property. Mutually exclusive buttons, such as JToggleButton s 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
已经改变。豆子的规范表示null
属性名称可以用来表示多个值已经改变了。默认情况下,Swing组件以Action
不应对这种变化。表明摆动应该null
根据豆规范设置系统属性的值true
String
swing.actions.reconfigureOnNull
。
AbstractAction
Modifier and Type | Field and Description |
---|---|
static String |
ACCELERATOR_KEY
用于存储
KeyStroke 作为加速器的关键作用。
|
static String |
ACTION_COMMAND_KEY
主要用于确定命令
String 为
ActionEvent 将会创造一个
Action 时将居住在一个
JComponent 相关
Keymap 结果通知。
|
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 启用状态。
|
actionPerformed
static 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
时将居住在一个
JComponent
相关
Keymap
结果通知。
static final String ACCELERATOR_KEY
KeyStroke
作为加速器的关键作用。
static final String MNEMONIC_KEY
Integer
,对应的一个
KeyEvent
关键代码的关键。该值通常用于指定一个记忆。例如:
myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)
套
myAction
'一'的记忆,而
myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar('?'))
套
myAction
记忆以斯拉夫字母“EF”。
static final String SELECTED_KEY
Boolean
对应于选定状态的关键。这是通常只用于有一个有意义的选择状态的组件。例如,
JRadioButton
和
JCheckBox
利用但不
JMenu
此属性与其他属性不同,它是由组件读取的,并由组件设置的。例如,如果一个Action
是连接到一个JCheckBox
的JCheckBox
选定状态将从该Action
。如果在JCheckBox
的JCheckBox
选定状态和的Action
将二者都更新用户点击。
注:此字段的值的前缀是“摇摆”以避免可能的冲突与现有的Actions
。
static final String DISPLAYED_MNEMONIC_INDEX_KEY
Integer
对应的索引文本的关键(由
NAME
属性确定),一个记忆的装饰应渲染。如果此属性的值大于或等于文本的长度,则将视为- 1。
注:此字段的值的前缀是“摇摆”以避免可能的冲突与现有的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
。
注:此字段的值的前缀是“摇摆”以避免可能的冲突与现有的Actions
。
void putValue(String key, Object value)
PropertyChangeEvent
送给听众。
key
-
String
含有关键
value
-
Object
价值
void setEnabled(boolean b)
Action
启用状态。当启用时,与该对象相关的任何部分是积极的、能火这个对象的
actionPerformed
方法。如果值发生了变化,一
PropertyChangeEvent
送给听众。
b
真正使这
Action
,假禁用它
boolean isEnabled()
Action
启用状态。当启用时,与该对象相关的任何部分是积极的、能火这个对象的
actionPerformed
方法。
Action
启用
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.