简体   繁体   English

我在哪里可以找到针对Web开发人员的Java Swing GUI的优秀,简短,面向架构的介绍?

[英]Where do I find a good, short, architecture-oriented introduction to Java Swing GUI for web developerrs?

I'm just starting my first Java Swing project(doing mainly web-based apps before) and try to understand how to build a proper architecture with separation of concerns between the MVC components. 我刚刚开始我的第一个Java Swing项目(以前主要做基于Web的应用程序),并试图了解如何构建一个适当的架构,并在MVC组件之间分离关注点。

Just about any documentation I find goes very deep into the details of how each and every Swing UI widget works and can be used, but all examples just directly call program logic from a Class that extends, for example, JPanel - which seems odd and no good architecure. 几乎我发现的任何文档都深入探讨了每个Swing UI小部件如何工作和可以使用的细节,但所有示例只是直接从扩展的类中调用程序逻辑,例如,JPanel - 这似乎很奇怪而且没有良好的建筑。

It would be best if that would be IDE-independent, but if such things come into play, it should be said that in the overall project, we have already Eclipse, JFormdesigner and JGoodies in use. 如果它与IDE无关,那将是最好的,但如果这些事情发挥作用,应该说在整个项目中,我们已经使用了Eclipse,JFormdesigner和JGoodies。

I also see that JSR296 defines a framework that seems to address my concerns. 我还看到JSR296定义了一个似乎可以解决我的问题的框架。 Should I just use something that implements it? 我应该只使用实现它的东西吗?

This is an area of Java programming that is highly under-documented. 这是一个Java编程领域,其文档资料严重不足。 As you mention, extending from JFrame or JDialog to develop a GUI is not a good design practice, yet you see it all over the place in sample code. 正如您所提到的,从JFrame或JDialog扩展到开发GUI并不是一个好的设计实践,但您可以在示例代码中看到它。

JSR 296 is a useful starting place, but it's architecture has some serious problems. JSR 296是一个有用的起点,但它的架构有一些严重的问题。 I do use JSR 296, but I have my own flavor of it, and consistently have to work around issues introduced by the framework design. 我确实使用JSR 296,但我有自己的风格,并始终不得不解决框架设计引入的问题。

I've long thought that there should be a discussion group/wiki/something focused on this topic. 我一直认为应该有一个讨论组/维基/关注这个主题的东西。 So far, I've found the listserv for various rich client libraries to be useful, but not comprehensive. 到目前为止,我发现各种富客户端库的listserv非常有用,但并不全面。 Something to think about starting maybe, in my free time :-) 在我的空闲时间考虑开始的事情:-)

So I can't provide any definitive resources for best practices in building swing applications. 因此,我无法为构建秋千应用程序的最佳实践提供任何明确的资源。 But I can give you some pointers to the toolkits and concepts that I've found that I use over and over again. 但是我可以给你一些关于我发现我一遍又一遍地使用的工具包和概念的指示。 Maybe these will be useful to you as you get going. 也许这些对你来说很有用。 Also, if enough people are interested in having a discussion about best practices, sharing code, etc... I'd be interested in being part of it. 此外,如果有足够的人有兴趣讨论最佳实践,共享代码等...我有兴趣成为其中的一部分。

First, some absolutely critical libraries if you are going to do Swing development: 首先,如果您要进行Swing开发,一些绝对重要的库:

  1. Binding - there are a number of libraries that do this (JGoodies, JSR295 which has spun off into an open source project called Better Beans Binding (BBB), Eclipse binding framework). 绑定 - 有许多库可以做到这一点(JGoodies,JSR295已分解为一个名为Better Beans Binding(BBB)的开源项目,Eclipse绑定框架)。 I started years ago using JGoodies, but I have moved over to using BBB because I find it's approach to be more intuitive. 我几年前开始使用JGoodies,但我已经转向使用BBB,因为我发现它的方法更直观。 I can't stress the advantages of the declarative coding approach that binding allows for - it will truly revolutionize your code 我无法强调绑定允许的声明性编码方法的优点 - 它将真正彻底改变您的代码

  2. AppFramework (or some flavor thereof) - JSR 296 is the place to start here. AppFramework(或其中的一些风格) - JSR 296是从这里开始的地方。 As I mentioned above, it has some problems - if you do use JSR296, I strongly, strongly recommend that you try to avoid using the singleton that is at the core of the framework (other than as a source for injection of the framework components that you actually need). 正如我上面提到的,它有一些问题 - 如果你确实使用JSR296,我强烈建议你尽量避免使用框架核心的单例(除了作为注入框架组件的源代码)你真的需要)。

EDIT - since I wrote this, I've started using GUTS in our projects (this is a Guice based app framework - it started life as JSR 296, but has very little in common with it now). 编辑 - 自从我写这篇文章以来,我开始在我们的项目中使用GUTS (这是一个基于Guice的应用程序框架 - 它起源于JSR 296,但现在与它几乎没有共同之处)。 GUTS is still a young project, but it's worth taking a look at if you are considering frameworks. GUTS仍然是一个年轻的项目,但值得一看,如果你正在考虑框架。

  1. GlazedLists - if you are doing anything in the UI that involves lists, tables or trees, you should take a hard look at GlazedLists. GlazedLists - 如果您在UI中执行涉及列表,表格或树的任何操作,您应该仔细查看GlazedLists。 It's an incredible project (not just for Swing apps, but it really shines in that arena) 这是一个令人难以置信的项目(不仅仅适用于Swing应用程序,但它真的在这个舞台上闪耀)

  2. Validation - JGoodies has a very good validation library. 验证 - JGoodies有一个非常好的验证库。 Learn it, use it, be one with it. 学习它,使用它,成为它的一员。 Real time validation is an incredibly important part of a modern Swing app. 实时验证是现代Swing应用程序中非常重要的一部分。

  3. MigLayout - The Mig layout manager is the best around. MigLayout - Mig布局管理器是最好的。 I strongly advise against the temptation of using an IDE GUI builder - learn MigLayout (it will take a couple of hours, tops), and code things up by hand. 我强烈反对使用IDE GUI构建器的诱惑 - 学习MigLayout(它需要几个小时,顶部),并手动编写代码。

So those are the key, absolutely must-have libraries in my book. 所以这些是我书中必不可少的关键库。

Now some concepts: 现在一些概念:

A. Presentation Model - Martin Fowler has a lot of info on this design pattern. A.演示模型 - Martin Fowler对此设计模式有很多信息。 Long and short, it separates behavior from presentation at the GUI level. 无论长短,它都将行为与GUI级别的呈现区分开来。 IF you are used to MVC, Presentation Model adds another layer of separation that is quite important to 'live' UIs. 如果您习惯于MVC,Presentation Model会添加另一层分离,这对于“实时”UI非常重要。 All of my views are backed by a corresponding presentation model. 我的所有观点都有相应的演示模型支持。 The end result is that the view code is really, really simple - focusing on two things: 1. Layout, and 2. Binding view components to the presentation model. 最终结果是视图代码真的非常简单 - 专注于两件事:1。布局,以及2.将视图组件绑定到表示模型。 That's it. 而已。

B. Views are NOT subclasses of JPanel. B.视图不是JPanel的子类。 Instead, I follow the JGoodies inspired technique of treating the View as a builder that creates JPanels. 相反,我遵循JGoodies启发的技术,将视图视为创建JPanels的构建器。 The basic pattern is: 基本模式是:

public class MyView{
  private MyPresentationModel model;

  private JButton okButton;
  private JButton cancelButton;
  ...

  public MyView(MyPresentationModel model){
    this.model = model;
  }

  public JPanel buildView(){
    initComponents(); // this method actually creates the okButton and cancelButton objects
    bindComponentsToModel(); // this method binds those objects to the PresentationModel
    JPanel p = new JPanel(new MigLayout());
    p.add(...);
    ...
    return p;
  }
}

This approach, followed religiously, allows incredibly rapid development of UIs that are easy to maintain. 这种方法遵循宗教信仰,允许非常快速地开发易于维护的UI。 Note that we can use a given View to construct multiple JPanels that are all backed by the same PresentationModel - changes in one panel generated by the view will immediately be visible in another panel generated by the same view. 请注意,我们可以使用给定的View来构造多个JPanel,这些JPanel都由相同的PresentationModel支持 - 视图生成的一个面板中的更改将立即在同一视图生成的另一个面板中可见。

C. Use Actions not event handlers. C.使用操作而不是事件处理程序。 JSR 296 actually does a good job of making Actions easy to create and work with. JSR 296实际上很好地使Actions易于创建和使用。

D. Do long running operations (even something that takes 100ms) off of the EDT. D.远离EDT的长期运行(甚至需要100ms的运行)。 JSR 296 makes this fairly easy with it's Task support - but there are a number of gotchas in 296's Task system when it comes to exception handling. JSR 296通过它的任务支持使这相当容易 - 但在涉及异常处理时,296的Task系统中有许多问题。 If you have property changes that in turn result in long running events, be sure you think carefully about which thread those changes are going to occur on. 如果您的属性更改反过来导致长时间运行的事件,请务必仔细考虑这些更改将发生在哪个线程上。 Using Tasks is a big change to how you do development, but it's a really important area for any real Swing application - take the time to learn about it. 使用任务是对开发方式的一个重大改变,但对于任何真正的Swing应用程序来说,它都是一个非常重要的领域 - 花时间去了解它。

E. Resource injection is important. E.资源注入很重要。 Use it from the beginning (instead of telling yourself that you'll add it later) - if you find yourself calling setText() on a JLabel, it's time to sit back and call setName() instead, and add an entry to the resources file. 从头开始使用它(而不是告诉自己以后会添加它) - 如果你发现自己在JLabel上调用了setText(),那么就该坐下来调用setName(),然后在资源中添加一个条目文件。 JSR 296 makes this pretty easy to do if you are disciplined about it. JSR 296使你很容易做到这一点。

I think that's enough for now - this is a highly complex subject with lot's of nuance. 我认为现在已经够了 - 这是一个非常复杂的主题,有很多细微之处。 I've spent the past 7 or 8 years banging my head into things that don't work - and I'm continually finding better ways to do even the things that work well for me. 在过去的7到8年里,我一直在努力探索那些不起作用的事情 - 而且我不断寻找更好的方法去做那些对我有用的事情。

The Java Tutorial has a Swing trail that is very good for getting the concepts straight. Java Tutorial有一个Swing跟踪,非常适合直接获取概念。

If you are used to web apps, you will find the GUI-model rather confusing until you have this straight. 如果你习惯了网络应用程序,你会发现GUI模型相当混乱,直到你完成这一点。

http://java.sun.com/docs/books/tutorial/uiswing/ http://java.sun.com/docs/books/tutorial/uiswing/

Martin Fowler has started a development on his Patterns of Enterprise Application Architecture book covering some patterns that were not covered in the original book, including GUI patterns . Martin Fowler已经开始开发他的企业应用程序架构模式书,其中涵盖了原始书中未涉及的一些模式,包括GUI模式

Although its contents are not exclusively Java oriented, they are very valuable. 虽然它的内容不仅仅是面向Java的,但它们非常有价值。

You can try the Spring Rich Client project . 您可以尝试Spring Rich Client项目 I have a very good experience with the rest of the spring portfolio. 我对其他春季产品组合有非常好的体验。

Another very basic, very simple howto showing things about this is there: 另一个非常基本的,非常简单的如何展示这方面的事情是:

http://www.macs.hw.ac.uk/guidebook/?name=Using%20The%20GUI&page=1 http://www.macs.hw.ac.uk/guidebook/?name=Using%20The%20GUI&page=1

I'm not sure if I like the idea of the Controller itself implementing ActionListener - on the other hand, if it wouldn't, I'd need an extra class for this, which again would need to know the controller and tell him things... 我不确定我是否喜欢Controller本身实现ActionListener的想法 - 另一方面,如果不这样做,我需要一个额外的类,这需要知道控制器并告诉他事情...

Maybe the problem is just that there are so many possible choices of how Swing components can communicate with a controller, and none of them looking really "right" at the first glance... 也许问题只是有很多可能的选择,Swing组件可以如何与控制器通信,而且乍一看它们中没有一个看起来真的“正确”......

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

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