在Swing应用程序中组织操作? [英] Organizing Actions in a Swing Application?

查看:96
本文介绍了在Swing应用程序中组织操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的应用程序有一个JFrame,其中约有15个操作存储为JFrame中的字段。每个动作都是一个匿名类,其中一些很长。

My current application has a JFrame with about 15 actions stored as fields within the JFrame. Each of the actions is an anonymous class and some of them are pretty long.

将动作分解为可能在名为actions的子包中的各自类是否常见?

Is it common to break actions into their own classes possibly within a sub-package called actions?

如果没有,这种复杂性通常如何被驯服?

If not, how's this complexity usually tamed?

谢谢

推荐答案

如果您的行为可以重复使用(例如,从键盘快捷键,其他菜单,其他对话框等),特别是如果它们可以直接在基础模型(而不是在UI上),通常最好不要将它们作为匿名类。

If it is possible that your actions could be reusable (e.g., from keyboard shortcuts, other menus, other dialogs, etc.) and especially if they can work directly on the underlying model (rather than on the UI), then it is generally better not to have them as anonymous classes.

而是创建一个单独的包,并为每个包创建类。

Rather, create a separate package, and create classes for each.

通常,直接实例化这些也是有意义的,而是有某种经理定义常量并初始化并返回动作集,这样你就可以,例如,在不同版本中提供不同的操作集,或仅为内部版本设置某些操作。

Often, it also makes sense to not instantiate these directly but rather have some sort of a manager that defines constants and initializes and returns sets of actions, so that you could, for example, offer different action sets at different versions or set certain actions only for internal releases.

最后,检查您的操作是否可以重构为类层次结构。它们通常可以保存代码复制,还可以帮助您增加健壮性(例如,在执行操作之前检查某些条件)。

Finally, check whether your actions can be refactored into a class hierarchy. They often can, which saves code replication, and also helps you add robustness (e.g., check for certain conditions before letting the action execute).

这篇关于在Swing应用程序中组织操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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