简体   繁体   中英

Purpose of the Action interface that extends the ActionListener in Swing

I am currently learning Swing through this book . It says this about the Action interface:

To simplify event handling, the Swing library extends the original ActionListener interface with the Action interface to store visual attributes with the event handler. 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. This includes notification of updates for the label when the Action is modified. The AbstractAction and TextAction classes are implementations of this concept.

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.

For example you can use the Action to create a JButton and a JMenuItem . Then if you change the state of the Action , the state of the button and menu item will change at the same time.

Read the section from the Swing tutorial on How to Use Actions for more information and working examples. The example specifically demonstrates how you can change the enabled state of the Action and both components will be affected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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