问题
By convention, where should the ActionListener for a user inteface go? I have several options but none of them seem quite right.
Shorthand:
- GUI - main class that contains all the JPanels/displayable objects
- Logic - main class that handles application logic
- I could in-line declare a new ActionListener in GUI, store it, and then it/pass its pointer around to where it's needed.
- I could make GUI itself implement ActionListener and pass a reference to itself to where it's needed.
- I could in-line declare a new ActionListener in the main logic (this makes sense since the actions the buttons do are logic that shouldn't be in the GUI) and then pass it to GUI which will pass it to where it's needed.
- I could write the logic in a whole new file GUIListener.java and declare GUIListener where it's needed/pass it around.
- Some other method, I'm sure there are tons.
回答1:
This basically poses a question of Separation of Concerns. In my opinion you should keep GUI, Controller and Model(bean) objects in separate files. Controller facilitating all the processing related to application.
来源:https://stackoverflow.com/questions/14179485/java-swing-where-should-the-actionlistener-go