Java swing - Where should the ActionListener go?

99封情书 提交于 2019-12-24 01:37:08

问题


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

  1. I could in-line declare a new ActionListener in GUI, store it, and then it/pass its pointer around to where it's needed.
  2. I could make GUI itself implement ActionListener and pass a reference to itself to where it's needed.
  3. 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.
  4. I could write the logic in a whole new file GUIListener.java and declare GUIListener where it's needed/pass it around.
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!