Java摇摆-ActionListener应该去哪里? [英] Java swing - Where should the ActionListener go?

查看:104
本文介绍了Java摇摆-ActionListener应该去哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照约定,用户界面的ActionListener应该放在哪里?我有几种选择,但似乎都不对。

By convention, where should the ActionListener for a user inteface go? I have several options but none of them seem quite right.

速记:


  • GUI-包含所有JPanels /可显示对象的主类

  • Logic-处理应用程序逻辑的主类


  1. 我可以在GUI中内联声明一个新的ActionListener,将其存储,然后将其指针传递到需要的地方

  2. 我可以使GUI本身实现ActionListener并将引用传递给需要的地方。

  3. 我可以内联声明一个新的ActionListener。在主要逻辑中(这是有道理的,因为按钮所执行的操作是不应该在GUI中进行的逻辑),然后将其传递到GUI,GUI会将其传递到需要的地方。

  4. 我可以在一个全新的文件GUIListener.java中编写逻辑,并在需要的地方声明GUIListener或将其传递给周围。

  5. 其他方法,我确定有很多方法。
  6. li>
  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.


解决方案

这基本上引起了关注点分离的问题。
我认为您应该将GUI,Controller和Model(bean)对象保留在单独的文件中。控制器促进与应用程序相关的所有处理。

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.

这篇关于Java摇摆-ActionListener应该去哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆