简体   繁体   English

扩展 Swing 中的 ActionListener 的 Action 接口的用途

[英]Purpose of the Action interface that extends the ActionListener in Swing

I am currently learning Swing through this book .我目前正在通过这本书学习 Swing 。 It says this about the Action interface:它是关于 Action 接口的:

To simplify event handling, the Swing library extends the original ActionListener interface with the Action interface to store visual attributes with the event handler.为了简化事件处理,Swing 库使用 Action 接口扩展了原始的 ActionListener 接口,以使用事件处理程序存储视觉属性。 This allows the creation of event handlers independent of visual components.这允许创建独立于可视组件的事件处理程序。 Then, when the Action is later associated with a component, the component automatically gets information (such as a button label) directly from the event handler.然后,当稍后将 Action 与组件关联时,组件会自动从事件处理程序中直接获取信息(例如按钮标签)。 This includes notification of updates for the label when the Action is modified.这包括在修改操作时通知 label 的更新。 The AbstractAction and TextAction classes are implementations of this concept. AbstractAction 和 TextAction 类是这个概念的实现。

Can somebody please explain what this text means?有人可以解释一下这段文字是什么意思吗? Thanks in advance.提前致谢。

Basically if means that properties of the Action can be shared by one (or more) components, so you only need to set the property once.基本上 if 意味着Action的属性可以被一个(或多个)组件共享,因此您只需要设置一次属性。

For example you can use the Action to create a JButton and a JMenuItem .例如,您可以使用Action创建JButtonJMenuItem Then if you change the state of the Action , the state of the button and menu item will change at the same time.那么如果你改变Action的state,按钮和菜单项的state会同时改变。

Read the section from the Swing tutorial on How to Use Actions for more information and working examples.阅读 Swing 教程中有关如何使用操作的部分以获取更多信息和工作示例。 The example specifically demonstrates how you can change the enabled state of the Action and both components will be affected.该示例具体演示了如何更改 Action 的启用 state 并且两个组件都会受到影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM