简体   繁体   English

GUI组件ActionListener的最佳实践?

[英]Best practise for GUI Components ActionListener?

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. 我做了一个很大的基于GUI的应用程序,我现在有许多Action东西......我有不同的Listeneres ,比如ActionListenerKeyAdapters ,......所有东西都应该运行线程,所以我的GUI在长时间操作时不会冻结。 So im using SwingWorker in every ActionListener ... Currently its working like this: I have my JComponents , bound on one single ActionListener . 所以我在每个ActionListener使用SwingWorker ...目前它的工作方式如下:我有一个JComponents ,绑定在一个ActionListener In this Listener I decide what to do, based on the actionCommand() . 在这个Listener我根据actionCommand()决定做什么。 The I call a method, which contains my SwingWorker and the Action which should be performed. 我调用一个方法,其中包含我的SwingWorker和应该执行的Action

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? 所以我决定用另一种方式做...我认为工厂方法会很棒,但这是我的问题:我有时需要来自GUI的数据,例如:当按下JButton x时, JTextField是什么等等......那么最佳做法是什么? Should I just give my Factory a instance of my complete GUI? 我应该给我的工厂一个完整的GUI实例吗? Or a ArrayList of Components ? 还是ComponentsArrayList 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? 另一个问题是需要从我的GUI更改值,例如:按下按钮x然后过滤JTable ......我该怎么做? doing it like this = myFactory.process(this); this = myFactory.process(this);this = myFactory.process(this); isnt really that what i want... 真的不是我想要的......

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). 看一下JGoodies Binding :它强调使用PM( PresentationModel ),其中存储所有GUI状态(并绑定到实际的GUI组件)。

Every View has an associated PM that makes the link with the domain model. 每个View都有一个关联的PM,它与域模型建立链接。 PM can live without the View (but the reverse is not true). PM可以在没有View的情况下生存(但反之则不然)。

PM should not have any GUI-related dependency (so that it is unit testable without the GUI), hence no reference to a JTextField, JButton... PM不应该有任何与GUI相关的依赖(因此它可以在没有GUI的情况下进行单元测试),因此不能引用JTextField,JButton ......

However, PM normally includes Action s that are attached to buttons from the View . 但是,PM通常包含附加到View中按钮的Action Actions are not actual GUI components (although they belong to javax.swing package. 操作不是实际的GUI组件(尽管它们属于javax.swing包。

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). 请注意,Karsten Lentzsch(JGoodies作者)在他的演讲中没有谈论这个案例(这是我自己处理这个案子的方式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM