-
- 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接口为Action接口提供了有用的扩展,ActionListener多个控件访问相同的功能。除了
ActionListener接口定义的actionPerformed方法ActionListener,此接口还允许应用程序在单个位置定义:- 一个或多个描述函数的文本字符串。 例如,可以使用这些字符串来显示按钮的悬浮文本或在菜单项中设置文本。
- 一个或多个描绘功能的图标。 这些图标可用于菜单控件中的图像,也可用于更复杂的用户界面中的复合条目。
- 功能的启用/禁用状态。 应用程序可以禁用实现此接口的功能,而不必单独禁用菜单项和工具栏按钮。 然后,所有注册为状态更改的侦听器的组件都知道禁用该项的事件生成并相应地修改显示。
此接口可以添加到现有类或用于创建适配器(通常通过子类
AbstractAction)。 所述Action然后对象可被添加到多个Action知晓容器和连接到Action-能够组件。 然后,可以通过调用Action对象的setEnabled方法一次激活或取消激活GUI控件。请注意,
Action实施在存储方面往往比典型的ActionListener更昂贵,后者不提供集中控制功能和广播属性更改的好处。 出于这个原因,你应该注意只使用Actions的地方,并在其他地方使用简单的ActionListener。摆动部件支持
ActionSwing的许多组件都具有
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已更改。 beans规范指示可以使用null属性名称来指示多个值已更改。 默认情况下,采用ActionSwing组件不会处理此类更改。 要指示Swing应根据bean规范处理null,请将系统属性swing.actions.reconfigureOnNull设置为String值true。- 从以下版本开始:
- 1.2
- 另请参见:
-
AbstractAction
-
-
字段汇总
字段 变量和类型 字段 描述 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 变量和类型 方法 描述 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的启用状态。-
声明方法的接口 java.awt.event.ActionListener
actionPerformed
-
-
-
-
字段详细信息
-
SMALL_ICON
static final String SMALL_ICON
用于存放小型Icon,如ImageIcon。 这通常用于菜单,如JMenuItem。如果相同的
Action与菜单和按钮一起使用,则通常会同时指定SMALL_ICON和LARGE_ICON_KEY。 菜单将使用SMALL_ICON,按钮将使用LARGE_ICON_KEY。- 另请参见:
- 常数字段值
-
ACTION_COMMAND_KEY
static final String ACTION_COMMAND_KEY
用于确定该命令的键String为ActionEvent,将当被创建Action将被通知作为驻留在的结果Keymap与相关联JComponent。- 另请参见:
- 常数字段值
-
ACCELERATOR_KEY
static final String ACCELERATOR_KEY
用于存储KeyStroke的密钥,用作动作的加速器。- 从以下版本开始:
- 1.3
- 另请参见:
- 常数字段值
-
MNEMONIC_KEY
static final String MNEMONIC_KEY
用于存储Integer的密钥,该密钥对应于KeyEvent密钥代码之一。 该值通常用于指定助记符。 例如:myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)设置的记忆myAction至“A”,而myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar('ф'))设置的记忆myAction以西里尔字母“EF”。- 从以下版本开始:
- 1.3
- 另请参见:
- 常数字段值
-
SELECTED_KEY
static final String SELECTED_KEY
用于存储与所选状态对应的Boolean的密钥。 这通常仅用于具有有意义选择状态的组件。 例如,JRadioButton和JCheckBox使用此,但JMenu实例没有。此属性与其他属性的不同之处在于它既由组件读取又由组件设置。 例如,如果一个
Action附着到JCheckBox所述的选择状态JCheckBox将从所述的设置Action。 如果用户点击JCheckBox的的选择状态JCheckBox和Action都将被更新。注意:此字段的值以“Swing”为前缀,以避免与现有
Actions发生冲突。- 从以下版本开始:
- 1.6
- 另请参见:
- 常数字段值
-
DISPLAYED_MNEMONIC_INDEX_KEY
static final String DISPLAYED_MNEMONIC_INDEX_KEY
用于存储Integer的密钥,该对应于文本中的索引(由NAME属性标识),应该呈现助记符的装饰。 如果此属性的值大于或等于文本的长度,则将其视为-1。注意:此字段的值以“Swing”为前缀,以避免与现有
Actions发生冲突。- 从以下版本开始:
- 1.6
- 另请参见:
-
AbstractButton.setDisplayedMnemonicIndex(int), 常数字段值
-
-
方法详细信息
-
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- 如果启用此Action,btrue;如果为false,Actionfalse - 另请参见:
-
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如果应使用sender对象执行操作,则在禁用操作时必须为false。 - 另请参见:
-
isEnabled(),setEnabled(boolean)
-
addPropertyChangeListener
void addPropertyChangeListener(PropertyChangeListener listener)
添加一个PropertyChange监听器。 容器和附加组件使用这些方法来注册对此Action对象的兴趣。 当其启用状态或其他属性发生更改时,已通知已注册的侦听器。- 参数
-
listener- 一个PropertyChangeListener对象
-
removePropertyChangeListener
void removePropertyChangeListener(PropertyChangeListener listener)
删除PropertyChange侦听器。- 参数
-
listener- 一个PropertyChangeListener对象 - 另请参见:
-
addPropertyChangeListener(java.beans.PropertyChangeListener)
-
-