用Java组织GUI和ActionListener的代码 [英] Organize Code for GUI and ActionListener in Java

查看:86
本文介绍了用Java组织GUI和ActionListener的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我想知道如何在项目中实现 ActionListener 代码。我知道内部类和 ActionListener 接口的实现,但是走那条路会使代码看起来比应该的看起来更混乱。



我会写另一个类 ActionCode 来实现 ActionListener 并扩展GUI还是你会走哪条路



您的最佳做法是什么?在哪里可以找到这些指南?
(JavaDoc似乎解释了 ActionListener s的基本实现,但似乎没有任何模型来组织大型/中型项目)。

解决方案

在我看来,没有最佳方法。甚至sun / oracle教程中的代码示例也使用不同的方法来实现侦听器。



根据我的经验,一个好的方法是:




  • 使用匿名实现:人们知道这种模式并将很快识别它。如果有通用的处理方法,它可以帮助读者理解代码

  • 具有一个特殊的方法,该方法仅处理侦听器(例如, private void addListeners( )):同样,这可以帮助每个人识别它并知道在哪里搜索所有逻辑

  • 使侦听器保持简单。这意味着少于5-10行代码。如果需要更复杂的逻辑,请调用方法。

  • 将侦听器的数量保持较小。如果需要> 50个侦听器,则可能应该重构视图。如果需要的数量超过10,则可以考虑进行重构。



除了这些一般要点外,总是有例外。就像如果您有很多具有相同行为的组件一样,您可以编写带有开关/案例的通用侦听器。 (典型示例:来自计算器的按钮或菜单按钮)。

或者,如果对多个组件具有相同的逻辑,则可以使用特定的类。

,依此类推。 / p>

只需提及一下,因为在sun / oracle教程中有一些示例:尝试避免使用视图类本身实现侦听器接口。如果您只有一个侦听器,则可以这样做,但是在大多数情况下,来自多个来源且行为不同的多个事件是很糟糕的。


I am new to Java and was wondering how I would go about implementing ActionListener code in my project. I am aware of inner classes and the implementation the ActionListener interface, but going down that road makes the code look more messy than it probably should.

Would I write another class ActionCode that implements ActionListener and extends GUI or what road would you suggest and why?

What's your best practice advise on that and where can I find those guidelines? (The JavaDoc seems to explain the basic implementation of ActionListeners, but doesn't seem to have any model how to organize large/medium projects).

解决方案

In my opinion, there is no "best" approach. Even the code examples from sun/oracle tutorials use different ways to implement listeners.

From my experience, a good approach is:

  • Use anonymous implementations: People know this pattern and will quickly recognize it. It helps the reader to understand the code if there is a common way to do things
  • Have a special method, which only handles the listeners (e.g. private void addListeners()): Again, this helps everyone to recognize it and to know where to search for all the logic
  • Keep the listeners simple. This means less than 5-10 lines of code. If you need more complex logic, call a method.
  • Keep the number of listeners small. If you need > 50 listeners, you should probably refactor your view. If you need more than 10, you could think about refactoring.

Beside this general points, there are always exceptions. Like if you have a lot of Components with the same behavior, you could write a generic listener with a switch/case. (Typical example: buttons from a calculator or menu buttons).
Or if you have the same logic for multiple components, you could use a specific class.
And so on.

And just to mention it, because there are some examples in the sun/oracle tutorials: Try to avoid implementing a listener interface with the view class itself. This could be ok if you have only one listener, but it is most of the times awful for multiple events from multiple source with different behavior.

这篇关于用Java组织GUI和ActionListener的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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