-
- All Superinterfaces:
-
ActionListener,EventListener
- 所有已知实现类:
-
AbstractAction,BasicDesktopPaneUI.CloseAction,BasicDesktopPaneUI.MaximizeAction,BasicDesktopPaneUI.MinimizeAction,BasicDesktopPaneUI.NavigateAction,BasicDesktopPaneUI.OpenAction,BasicFileChooserUI.ApproveSelectionAction,BasicFileChooserUI.CancelSelectionAction,BasicFileChooserUI.ChangeToParentDirectoryAction,BasicFileChooserUI.GoHomeAction,BasicFileChooserUI.NewFolderAction,BasicFileChooserUI.UpdateAction,BasicInternalFrameTitlePane.CloseAction,BasicInternalFrameTitlePane.IconifyAction,BasicInternalFrameTitlePane.MaximizeAction,BasicInternalFrameTitlePane.MoveAction,BasicInternalFrameTitlePane.RestoreAction,BasicInternalFrameTitlePane.SizeAction,BasicSliderUI.ActionScroller,BasicTreeUI.TreeCancelEditingAction,BasicTreeUI.TreeHomeAction,BasicTreeUI.TreeIncrementAction,BasicTreeUI.TreePageAction,BasicTreeUI.TreeToggleAction,BasicTreeUI.TreeTraverseAction,DefaultEditorKit.BeepAction,DefaultEditorKit.CopyAction,DefaultEditorKit.CutAction,DefaultEditorKit.DefaultKeyTypedAction,DefaultEditorKit.InsertBreakAction,DefaultEditorKit.InsertContentAction,DefaultEditorKit.InsertTabAction,DefaultEditorKit.PasteAction,HTMLEditorKit.HTMLTextAction,HTMLEditorKit.InsertHTMLTextAction,MetalFileChooserUI.DirectoryComboBoxAction,StyledEditorKit.AlignmentAction,StyledEditorKit.BoldAction,StyledEditorKit.FontFamilyAction,StyledEditorKit.FontSizeAction,StyledEditorKit.ForegroundAction,StyledEditorKit.ItalicAction,StyledEditorKit.StyledTextAction,StyledEditorKit.UnderlineAction,TextAction
public interface Action extends ActionListener
所述Action接口提供了一个有用的扩展到ActionListener在箱子接口,其中相同的功能可以由若干控件访问。除了由
ActionListener界面定义的actionPerformed方法ActionListener,此接口允许应用程序在单个位置定义:- 描述功能的一个或多个文本字符串。 例如,可以使用这些字符串来显示按钮的天桥文字或在菜单项中设置文本。
- 一个或多个描绘功能的图标。 这些图标可用于菜单控件中的图像,或用于更复杂的用户界面中的复合条目。
- 启用/禁用的功能状态。 而不必单独禁用菜单项和工具栏按钮,应用程序可以禁用实现此接口的功能。 注册为状态更改的监听器的所有组件都知道禁用该项目的事件生成并相应地修改显示。
此接口可以添加到现有类或用于创建适配器(通常通过子类化
AbstractAction)。 然后可以将Action对象添加到多个Action容器中,并连接到Action组件。 然后可以通过调用Action对象的setEnabled方法一次性激活或停用GUI控件。请注意,
Action实现在存储方面往往比典型的ActionListener更昂贵,这不具有集中控制功能和广播属性更改的好处。 因此,您应该注意只使用Action,其中需要其优点,并在其他地方使用简单的ActionListener。支持
Action摆动组件很多Swing的组件都有一个
Action属性。 当Action上设置了一个Action,会发生以下情况:- 所述
Action加入作为ActionListener到组件。 - 该组件配置其一些属性以匹配
Action。 - 该组件安装一个
PropertyChangeListener上Action,使得部件可以改变其性能以反映变化Action的属性。
下表介绍了支持
Supported Action properties Component Property Components Action Key NotesActionsSwing组件所使用的属性。 在表中, 按钮指的是任何AbstractButton子类,其中不仅包括JButton,还包括类,如JMenuItem。 除非另有说明,否则Action(或Action为null)中的null属性值将导致按钮的相应属性设置为null。enabledAll TheisEnabledmethodtoolTipTextAllSHORT_DESCRIPTIONactionCommandAllACTION_COMMAND_KEYmnemonicAll buttonsMNEMONIC_KEYAnullvalue orActionresults in the button'smnemonicproperty being set to'\0'.textAll buttonsNAMEIf you do not want the text of the button to mirror that of theAction, set the propertyhideActionTexttotrue. IfhideActionTextistrue, setting theActionchanges the text of the button tonulland any changes toNAMEare ignored.hideActionTextis useful for tool bar buttons that typically only show anIcon.JToolBar.add(Action)sets the property totrueif theActionhas a non-nullvalue forLARGE_ICON_KEYorSMALL_ICON.displayedMnemonicIndexAll buttonsDISPLAYED_MNEMONIC_INDEX_KEYIf the value ofDISPLAYED_MNEMONIC_INDEX_KEYis beyond the bounds of the text, it is ignored. WhensetActionis called, if the value from theActionisnull, the displayed mnemonic index is not updated. In any subsequent changes toDISPLAYED_MNEMONIC_INDEX_KEY,nullis treated as -1.iconAll buttons except ofJCheckBox,JToggleButtonandJRadioButton. eitherLARGE_ICON_KEYorSMALL_ICONTheJMenuItemsubclasses only useSMALL_ICON. All other buttons will useLARGE_ICON_KEY; if the value isnullthey useSMALL_ICON.acceleratorAllJMenuItemsubclasses, with the exception ofJMenu.ACCELERATOR_KEYselectedJToggleButton,JCheckBox,JRadioButton,JCheckBoxMenuItemandJRadioButtonMenuItemSELECTED_KEYComponents that honor this property only use the value if it isnon-null. For example, if you set anActionthat has anullvalue forSELECTED_KEYon aJToggleButton, theJToggleButtonwill not update it's selected state in any way. Similarly, any time theJToggleButton's selected state changes it will only set the value back on theActionif theActionhas anon-nullvalue forSELECTED_KEY.
Components that honor this property keep their selected state in sync with this property. When the sameActionis used with multiple components, all the components keep their selected state in sync with this property. Mutually exclusive buttons, such asJToggleButtons in aButtonGroup, force only one of the buttons to be selected. As such, do not use the sameActionthat defines a value for theSELECTED_KEYproperty with multiple mutually exclusive buttons.JPopupMenu,JToolBar和JMenu所有提供用于创建部件和设置方便的方法Action相应部件上。 有关更多信息,请参阅这些课程。Action使用PropertyChangeListener通知听众Action已更改。 bean规范表示可以使用null属性名来指示多个值已更改。 默认情况下,采用ActionSwing组件不会处理此类更改。 要指出Swing应该根据beans规定处理null,系统属性swing.actions.reconfigureOnNull设置为String值为true。- 从以下版本开始:
- 1.2
- 另请参见:
-
AbstractAction
-
-
Field Summary
Fields Modifier and Type Field 描述 static StringACCELERATOR_KEY用于存储KeyStroke作为加速器的动作的关键。static StringACTION_COMMAND_KEY用于确定String的ActionEvent的命令,将在Action将被通知作为与Keymap相关联的JComponent的结果时被创建。static StringDEFAULT目前不使用static StringDISPLAYED_MNEMONIC_INDEX_KEY用于存储的密钥Integer对应于在文本(由所识别的索引NAME属性),对于一个助记符的装饰应在被渲染。static StringLARGE_ICON_KEY用于存储Icon的密钥。static StringLONG_DESCRIPTION用于存储更长的String描述的关键字可用于上下文相关帮助。static StringMNEMONIC_KEY用于存储的密钥Integer对应于所述的一个KeyEvent键码。static StringNAME用于存储用于操作的String名称的键,用于菜单或按钮。static StringSELECTED_KEY用于存储对应于所选状态的Boolean的密钥。static StringSHORT_DESCRIPTION用于存储短暂的String描述的操作的键,用于工具提示文本。static StringSMALL_ICON用于存储小键Icon的键,如ImageIcon。
-
方法摘要
所有方法 接口方法 抽象方法 Default Methods Modifier and Type 方法 描述 default booleanaccept(Object sender)确定是否应该使用指定的发件人对象执行操作。voidaddPropertyChangeListener(PropertyChangeListener listener)添加一个PropertyChange监听器。ObjectgetValue(String key)使用关联的键获取该对象的属性之一。booleanisEnabled()返回启用状态的Action。voidputValue(String key, Object value)使用关联的键设置此对象的属性之一。voidremovePropertyChangeListener(PropertyChangeListener listener)删除一个PropertyChange监听器。voidsetEnabled(boolean b)设置Action的启用状态。-
Methods inherited from interface java.awt.event.ActionListener
actionPerformed
-
-
-
-
字段详细信息
-
DEFAULT
static final String DEFAULT
目前不使用- 另请参见:
- Constant Field Values
-
NAME
static final String NAME
用于存储String名称的键,用于菜单或按钮。- 另请参见:
- Constant Field Values
-
SHORT_DESCRIPTION
static final String SHORT_DESCRIPTION
用于存储用于操作的简短String描述的键,用于工具提示文本。- 另请参见:
- Constant Field Values
-
LONG_DESCRIPTION
static final String LONG_DESCRIPTION
用于存储更长时间的String描述的关键字可用于上下文相关帮助。- 另请参见:
- Constant Field Values
-
SMALL_ICON
static final String SMALL_ICON
用于存储小型Icon的键,如ImageIcon。 这通常用于菜单,如JMenuItem。如果与菜单和按钮使用相同的
Action,通常会同时指定SMALL_ICON和LARGE_ICON_KEY。 菜单将使用SMALL_ICON,按钮将使用LARGE_ICON_KEY。- 另请参见:
- Constant Field Values
-
ACTION_COMMAND_KEY
static final String ACTION_COMMAND_KEY
用于确定String的命令String的ActionEvent,将在Action被通知作为与Keymap相关联的JComponent的结果时被创建。- 另请参见:
- Constant Field Values
-
ACCELERATOR_KEY
static final String ACCELERATOR_KEY
用于存储KeyStroke用于作为加速器的动作的键。- 从以下版本开始:
- 1.3
- 另请参见:
- Constant Field Values
-
MNEMONIC_KEY
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.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar('?'))的助记符myAction为西里尔字母“Ef”。- 从以下版本开始:
- 1.3
- 另请参见:
- Constant Field Values
-
SELECTED_KEY
static final String SELECTED_KEY
用于存储对应于所选状态的Boolean的密钥。 这通常仅用于具有有意义的选择状态的组件。 例如,JRadioButton和JCheckBox使用这个但是JMenu实例没有。该属性与其他属性不同,它们由组件读取并由组件设置。 例如,如果一个
Action附着到JCheckBox所述的选择状态JCheckBox将从所述的设置Action。 如果用户点击JCheckBox的的选择状态JCheckBox和Action都将被更新。注意:此字段的值以“Swing”为前缀,以避免与现有的
Actions发生冲突。- 从以下版本开始:
- 1.6
- 另请参见:
- Constant Field Values
-
DISPLAYED_MNEMONIC_INDEX_KEY
static final String DISPLAYED_MNEMONIC_INDEX_KEY
用于存储Integer的密钥对应于文本中的索引(由NAME属性标识),助记符的装饰应在其上呈现。 如果此属性的值大于或等于文本的长度,则将其视为-1。注意:此字段的值以“Swing”为前缀,以避免与现有的
Actions发生冲突。- 从以下版本开始:
- 1.6
- 另请参见:
-
AbstractButton.setDisplayedMnemonicIndex(int), Constant Field Values
-
LARGE_ICON_KEY
static final String LARGE_ICON_KEY
用于存储Icon的密钥。 这通常由按钮使用,例如JButton和JToggleButton。如果与菜单和按钮使用相同的
Action,通常会同时指定SMALL_ICON和LARGE_ICON_KEY。 菜单将使用SMALL_ICON和按钮LARGE_ICON_KEY。注意:该字段的值以“Swing”为前缀,以避免与现有的
Actions发生冲突。- 从以下版本开始:
- 1.6
- 另请参见:
- Constant Field Values
-
-
方法详细信息
-
getValue
Object getValue(String key)
使用关联的键获取该对象的属性之一。- 参数
-
key- 一个String包含密钥 - 结果
-
Object价值 - 另请参见:
-
putValue(java.lang.String, java.lang.Object)
-
putValue
void putValue(String key, Object value)
使用关联的键设置此对象的属性之一。 如果值已更改,PropertyChangeEvent向侦听器发送PropertyChangeEvent。- 参数
-
key- 一个String包含密钥 -
value- 一个Object价值
-
setEnabled
void setEnabled(boolean b)
设置Action的启用状态。 启用后,与此对象关联的任何组件都处于活动状态,并且可以触发此对象的actionPerformed方法。 如果值已更改,PropertyChangeEvent向侦听器发送PropertyChangeEvent。- 参数
-
b- true启用此Action,false禁用它 - 另请参见:
-
accept(java.lang.Object)
-
isEnabled
boolean isEnabled()
返回启用状态的Action。 启用后,与此对象关联的任何组件都将处于活动状态,并能够触发此对象的actionPerformed方法。- 结果
-
如果启用了这个
Action则为true - 另请参见:
-
accept(java.lang.Object)
-
accept
default boolean accept(Object sender)
确定是否应该使用指定的发件人对象执行操作。sender可以是null。 如果操作被禁用,该方法必须返回false。- 参数
-
sender- 要检查的对象,可以为null - 结果
-
true如果该操作应该与发件人对象一起执行,则如果该操作被禁用则必须为false。 - 另请参见:
-
isEnabled(),setEnabled(boolean)
-
addPropertyChangeListener
void addPropertyChangeListener(PropertyChangeListener listener)
添加一个PropertyChange监听器。 容器和附加组件使用这些方法注册这个Action对象的兴趣。 当其启用状态或其他属性发生变化时,会向注册的听众通知更改。- 参数
-
listener- aPropertyChangeListener对象
-
removePropertyChangeListener
void removePropertyChangeListener(PropertyChangeListener listener)
删除一个PropertyChange监听器。- 参数
-
listener- aPropertyChangeListener对象 - 另请参见:
-
addPropertyChangeListener(java.beans.PropertyChangeListener)
-
-