Interface Action

Interface Action

Action接口为Action接口提供了有用的扩展, ActionListener多个控件访问相同的功能。

除了ActionListener接口定义的actionPerformed方法ActionListener ,此接口还允许应用程序在单个位置定义:

一个或多个描述函数的文本字符串。 例如,可以使用这些字符串来显示按钮的悬浮文本或在菜单项中设置文本。

一个或多个描绘功能的图标。 这些图标可用于菜单控件中的图像,也可用于更复杂的用户界面中的复合条目。

功能的启用/禁用状态。 应用程序可以禁用实现此接口的功能,而不必单独禁用菜单项和工具栏按钮。 然后,所有注册为状态更改的侦听器的组件都知道禁用该项的事件生成并相应地修改显示。

此接口可以添加到现有类或用于创建适配器(通常通过子类AbstractAction )。 所述Action然后对象可被添加到多个Action知晓容器和连接到Action -能够组件。 然后,可以通过调用Action对象的setEnabled方法一次激活或取消激活GUI控件。

请注意, Action实施在存储方面往往比典型的ActionListener更昂贵,后者不提供集中控制功能和广播属性更改的好处。 出于这个原因,你应该注意只使用Action s的地方,并在其他地方使用简单的ActionListener 。

摆动部件支持Action

Swing的许多组件都具有Action属性。 在Action上设置Action ,会发生以下情况:

所述Action被添加为ActionListener到组件。

该组件配置其某些属性以匹配Action 。

该组件安装一个PropertyChangeListener的Action ,使得部件可以改变其性能以反映变化Action的属性。

下表描述了支持Actions Swing组件使用的属性。 在表中, 按钮是指任何AbstractButton亚类,它不仅包括JButton而且类,例如JMenuItem 。 除非另有说明,否则Action (或Action ,即null )中的null属性值会导致按钮的相应属性设置为null 。

Supported Action properties 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 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已更改。 beans规范指示可以使用null属性名称来指示多个值已更改。 默认情况下,采用Action Swing组件不会处理此类更改。 要指示Swing应根据bean规范处理null ,请将系统属性swing.actions.reconfigureOnNull设置为String值true 。

💡 关键要点

Action接口为Action接口提供了有用的扩展, ActionListener多个控件访问相同的功能。 除了ActionListener接口定义的actionPerformed方法ActionListener ,此接口

更多疯狂内容

方舟砂矿兽有什么作用

方舟砂矿兽有什么作用

🔥 19 📅 06-28
方舟砂矿兽有什么作用

方舟砂矿兽有什么作用

🔥 19 📅 06-28
小米盒子适配哪些电视?选购指南及常见问题解答