質問

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.
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top