GUI 组件 ActionListener 的最佳实践?
我做了一个基于 GUI 的大型应用程序,现在我周围有很多 Action
东西...... 我有不同的Listeneres
,例如ActionListener
,KeyAdapters
,... 一切都应该以线程方式运行,这样我的 GUI 在进行长时间操作时不会冻结。所以我在每个 ActionListener
中使用 SwingWorker
... 目前它的工作方式如下:我有我的 JComponents
,绑定在一个 ActionListener
上。在此Listener
中,我根据actionCommand()
决定要做什么。我调用一个方法,其中包含我的 SwingWorker
和应该执行的 Action
。
我不再喜欢这个概念了,因为我的课程越来越长,而且我对所有功能都没有一个概述。所以我决定用另一种方式来做... 我认为工厂方法会是一件很棒的事情,但这是我的问题:我有时需要来自 GUI 的数据,例如:当按 JButton
x 时,JTextField
等等... 那么对此的最佳实践是什么?我应该给我的 Factory 一个完整的 GUI 实例吗?或者 Components
的 ArrayList
?另一个问题是需要更改 GUI 中的值,例如:按按钮 x,然后过滤 JTable
...我应该怎么做? 像 this = myFactory.process(this);
那样做并不是我真正想要的......
I did a big GUI Based App and I have now many many Action
stuff around there...
I have different Listeneres
, like ActionListener
, KeyAdapters
, ...
Everything should run threaded, so my GUI isnt freezing when do long time operations. So im using SwingWorker
in every ActionListener
...
Currently its working like this: I have my JComponents
, bound on one single ActionListener
. In this Listener
I decide what to do, based on the actionCommand()
. The I call a method, which contains my SwingWorker
and the Action
which should be performed.
Ii dont like this concept anymore, because my class is getting longer and longer and i dont have an overview about all the functionallity. So I decided to do it another way...
I thought Factory Methods would be a great thing, but here is my problem with them: I need sometimes data from my GUI, eg: when pressing JButton
x, what is in JTextField
y and so on...
so what is the best practise for this? Should I just give my Factory a instance of my complete GUI? Or a ArrayList
of Components
? Another problem is that need to change values from my GUI, eg: press button x and then filter a JTable
... how should i do that?
doing it like this = myFactory.process(this);
isnt really that what i want...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下JGoodies Binding:它强调使用 PM (PresentationModel),其中存储所有 GUI 状态(并绑定到实际的 GUI 组件)。
每个视图都有一个关联的PM,用于与域模型建立链接。 PM 可以在没有 View 的情况下生存(但反之则不然)。
PM 不应具有任何与 GUI 相关的依赖项(以便无需 GUI 即可对其进行单元测试),因此不引用 JTextField、JButton...
但是,PM 通常包含附加到的
Action
视图中的按钮。 Actions 不是实际的 GUI 组件(尽管它们属于javax.swing
包。我说“正常”是因为有些 Actions 可能需要显示消息框、打开新窗口...此类 actions然后应该放在另一个班级中。请注意,Karsten Lentzsch(JGoodies 作者)在他的演示中没有谈论这个案例(这是我自己处理这个案例的方式)。
Take a look at JGoodies Binding: it emphasizes the use of PM (PresentationModel) where all the GUI state is stored (and bound to the actual GUI components).
Every View has an associated PM that makes the link with the domain model. PM can live without the View (but the reverse is not true).
PM should not have any GUI-related dependency (so that it is unit testable without the GUI), hence no reference to a JTextField, JButton...
However, PM normally includes
Action
s that are attached to buttons from the View. Actions are not actual GUI components (although they belong tojavax.swing
package.I said "normally" because some Actions may need to display a message box, open a new window... This kind of actions should then be put in another class. Note that Karsten Lentzsch (JGoodies author) doesn't talk about this case in his presentations (this is my own way of dealing with this case).