简体   繁体   中英

Can I add a referenced JButton to a second JPanel?

Basically, i have a button that works fine within one panel. I want the exact same button to display concurrently in another JPanel and react to the same actionlistener etc.

A component can only belong to a single parent, so you cannot add a button to two or more containers.

You can, however, use a ActionListener on multiple buttons.

A simple solution would be use an Action . An Action is a self contained unit of work, it carries with it the configuration details and actionPerformed handler.

You could create two separate instance of the Action and apply it to two different JButton s and get the same visual details and they would carry out the same work when triggered...

See How to Use Actions

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