Java 架构 - 关于 ActionListener 约定的问题
我正在制作一个显示图形和操作图形的用户界面。该类扩展了 JFrame 并实现了 ActionListener。然后,ActionListener 调用不同的类来根据操作操作图形。当班级只有很少的 ActionListener 时,这很有效;然而,现在班级正变得难以管理。
我知道为了封装的目的,最好在用户界面类中包含 ActionListener,因为它需要访问界面的非静态组件。然而,封装性和可读性之间似乎存在冲突。
我建议将类分解为一个用于接口的类和第二个用于 ActionListener 的类,并静态访问接口组件。我想知道这是否遵循基本的设计惯例?而且,如果这是一种可接受的方法,您会将主类放在用户界面类还是 ActionListener 类中?
I am making a user interface which shows graphs and manipulates graphs. The class extends JFrame implements ActionListener. The ActionListener then calls different classes to manipulate graphs depending on the action. This worked while the class had few ActionListeners; however, now the class is becoming unmanageable.
I know that in the interest of encapsulation, it would be best to have the ActionListener within the user interface class because it needs to access non-static components of the interface. However, it seems like there is a conflict between encapsulation and readability.
What I am proposing is breaking the class into one class for the interface and a second for the ActionListener and accessing the interface components statically. What I want to know is does this follow basic design conventions? And, if this is an acceptable approach would you place the main class in the user-interface class or the ActionListener class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是重复的问题...但我的回答应该有助于解决您的问题。
简而言之,我的偏好是让 JFrame 类不实现 ActionListener,然后在 JFrame 中拥有许多实现 ActionListener 的命名内部类。
我会将 main 放在一个类中......并将其称为 Main。
下面是一些我喜欢的示例代码:
然后是 GUI:
Not a duplicate question... but my answer should help with your question.
Short summery, my preference would be to have the JFrame class not implement ActionListener and then have a number of named inner classes withing the JFrame that do implement the ActionListener.
I would place the main in a class unto itself... and call it Main.
Here is some sample code for the way I like to do it:
and then the GUI: