简体   繁体   中英

Action or ActionListener for MVC design pattern

I am writing a Java GUI application using the MVC design pattern. At first, I was putting the ActionListener classes for the JButtons as subclasses within the Controller. Then, I decided that I wanted the user to have the choice to be able to press a button OR the ENTER key to submit text in a text field. I read online that Action is better than ActionListener when you want more than one button click and key stroke to perform the same action.

I'm a little confused about where I should place the Action classes that extend AbstractAction when using the MVC pattern. Should they go in the in the Controller as subclasses the same way I was originally doing the ActionListener classes?

Also, If I place the Action classes as subclasses, my Controller will be pretty full of subclasses since I'll have a lot of buttons in the GUI. Is this the best practice?

I ended up keeping the Action classes in the Controller and made the subclasses static like the answer to this question . I'm still not totally sure if this is the best practice, but everything works and I can reuse the classes for buttons and key strokes that perform the same action.

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