java 如何组织 Swing GUI 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6269851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:07:23  来源:igfitidea点击:

How to organize a Swing GUI application?

javaswinguser-interfacearchitecture

提问by bln-tom

I've written a few GUI's using Swing and I know about MVC, but I never found a good way to really organize my code somehow. What I am looking for is something like the folder structure that maven introduces for each new project. Another example is rails, where MVC is introduced through the folder structure automatically. Is there something similar for Swing?

我已经使用 Swing 编写了一些 GUI 并且我了解 MVC,但是我从来没有找到真正以某种方式组织我的代码的好方法。我正在寻找的是类似于 maven 为每个新项目引入的文件夹结构。另一个例子是 rails,其中 MVC 是通过文件夹结构自动引入的。Swing 有类似的东西吗?

It would also be nice to see a book that describes the development of a larger Swing Application. All I find are books about design-guidelines where design refers to the look of the application. Other Swing books (like O'Reilly) describe in detail all the swing components, but where is any information about the big picture?

看到一本描述大型 Swing 应用程序开发的书也会很高兴。我找到的都是关于设计指南的书籍,其中设计是指应用程序的外观。其他 Swing 书籍(如 O'Reilly)详细描述了所有 Swing 组件,但哪里有关于全局的信息?

Are there any good examples of a swing gui, where you'd say "That's how you organize code/folders/packages for swing!"?

有没有很好的swing gui例子,你会说“这就是你组织swing代码/文件夹/包的方式!”?

EDIT: I found the following site http://www.ibm.com/developerworks/java/tutorials/j-springswing/section7.htmlwhich describes the usage of spring while creating a GUI. It is a quite old example and it doesn't answer my question, but it is a step into the right direction. It also mentions Spring RCP, but I'm not sure if it could be the solution.

编辑:我找到了以下站点http://www.ibm.com/developerworks/java/tutorials/j-springswing/section7.html,它描述了在创建 GUI 时使用 spring。这是一个相当古老的例子,它没有回答我的问题,但它是朝着正确方向迈出的一步。它还提到了 Spring RCP,但我不确定它是否可以成为解决方案。

EDIT2: I still didn't find any better answers. Does anybody know an example for a ideally structured Swing GUI which is Open Source? Does anybody know a book, which describes it? And if not for Swing, then maybe for GUI's in general?

EDIT2:我仍然没有找到更好的答案。有没有人知道一个理想结构的 Swing GUI 的例子,它是开源的?有没有人知道一本书,描述它?如果不是用于 Swing,那么可能是用于一般的 GUI 吗?

回答by jfpoilpret

Although it is Groovy, not Java, I would advise you take a look at Griffon, which is a "Grails for Swing".

尽管它是Groovy而不是Java,我还是建议您看看Griffon,它是“ Grails for Swing”。

It enforces a given structure (in terms of directories and patterns, MVCin particular) to all applications you build with it.

它对您使用它构建的所有应用程序强制执行给定的结构(在目录和模式方面,尤其是MVC)。

I think it can give you good ideas in general, although you would have to perform some little adaptation to Java.

我认为它可以为您提供一般的好主意,尽管您必须对 Java 进行一些小的调整。

Besides, please note that Griffon also supports Application building in Java, and it may also provide "archetypes" for that, so you could check that as well.

此外,请注意 Griffon 还支持 Java 中的应用程序构建,并且它也可能为此提供“原型”,因此您也可以检查一下。

回答by Jonas

You can read A Swing Architecture Overview. But when I do larger Swing applications I don't organize them in the same way as I do when doing web-development. I merely organize my Swing applications in functional parts. E.g. if the app has four tabs with forms and tables, I put the components from one tab in one package. Then I usually ends up with a few custom implementations of AbstractTableModelthat the forms and tables interact with. And the Model keeps a cache of the data and communicates with the database.

您可以阅读Swing 架构概述。但是当我做更大的 Swing 应用程序时,我不会像做 web 开发时那样组织它们。我只是在功能部分中组织我的 Swing 应用程序。例如,如果应用程序有四个带有表单和表格的选项卡,我会将一个选项卡中的组件放在一个包中。然后我通常会得到一些AbstractTableModel表单和表格与之交互的自定义实现。模型保持数据的缓存并与数据库进行通信。

In my latest Swing application, I used Akka actorsto simplify the threading. Then I had all my Swing components executed on the EDT-thread and they communicated with a Data Access Object that was a custom Actor. Then I also had a few TimerTaskthat synchronized data from the database with services on Internet, but they were never run on the EDT, they only communicated with the DAO, (my Actor).

在我最新的 Swing 应用程序中,我使用Akka actor来简化线程。然后我在 EDT 线程上执行我所有的 Swing 组件,并且它们与作为自定义 Actor 的数据访问对象进行通信。然后我还有一些TimerTask将数据库中的数据与 Internet 上的服务同步,但它们从未在 EDT 上运行,它们只与 DAO(我的 Actor)通信。

回答by Costis Aivalis

Web-development forces you to certain source and resource layouts. This is not the case with Swing applications. Here you can use the layout that suits your goals the best.

Web 开发迫使您使用某些源和资源布局。Swing 应用程序不是这种情况。在这里,您可以使用最适合您目标的布局。

You will be a lot more productive if you use some IDE. NetBeans is the perfect choice for Swing. NB gives you some restrictions on how to store your classes. There is a "Source Packages" folder which contains the Java Packages and a "Test Packages" for the corresponding JUnit test classes if you use JUnit, which i strongly advise you to do.

如果您使用某些 IDE,您的工作效率会更高。NetBeans 是 Swing 的完美选择。NB 给你一些关于如何存储你的类的限制。如果您使用 JUnit,则有一个“源包”文件夹,其中包含 Java 包和相应 JUnit 测试类的“测试包”,我强烈建议您这样做。

In case you use Hibernate, you must stick to its conventions, build a whatever.entity and whatever.util and the configuration XML files need to be in the 'default package'.

如果您使用 Hibernate,您必须遵守其约定,构建一个whatever.entity 和whatever.util,并且配置 XML 文件需要在“默认包”中。

Any further organization of these packages is up to programmer. I make a package with the JFrame that runs the whole show. And a package for every logical unit. Sometimes i use a package for the AbstractTableModels and keep them together and another for ComboBoxModels. It is not necessary to make deeper structures. You can have a separate package for the icons you may use for your buttons, one for sounds etc. Netbeans creates the runnable jar file for your application where all necessary files including the needed library jars are included.

这些包的任何进一步组织取决于程序员。我用运行整个节目的 JFrame 制作了一个包。每个逻辑单元都有一个包。有时我为 AbstractTableModels 使用一个包并将它们放在一起,另一个用于 ComboBoxModels。没有必要制作更深的结构。你可以有一个单独的图标包,用于按钮,一个用于声音等。Netbeans 为你的应用程序创建可运行的 jar 文件,其中包含所有必需的文件,包括所需的库 jars。

Here you have a little example. All names of the packages except 'th' are quite obvious. 'th' is the name of this application and i have the JFrame that hosts the application in there. This application uses iReport in order to generate reports. This is what the 'iReports' package is for.

这里有一个小例子。除了 'th' 之外,所有包的名称都很明显。'th' 是该应用程序的名称,我在其中拥有托管该应用程序的 JFrame。此应用程序使用 iReport 来生成报告。这就是“iReports”包的用途。

enter image description here

在此处输入图片说明

Books describe smaller samples and are more concerned with the Java conventions.

书籍描述了较小的示例,并且更关注 Java 约定。

You could try Apache Mavenand see the architecture they provide with the application archetypes.

您可以尝试Apache Maven并查看它们随应用程序原型提供的架构。

回答by Jér?me Verstrynge

Unfortunately (or fortunately, it depends how you take a look at it), it is not easy to develop generic panes usable for several data structures. M is often tightly related to V, and V often contains some C.

不幸的是(或者幸运的是,这取决于您如何看待它),开发可用于多种数据结构的通用窗格并不容易。M 通常与 V 紧密相关,而 V 中通常包含一些 C。

I tend to put the V pane and the corresponding M structure in the same package. You also need some central controlling displayer to display each individual panes. The C is shared between the central controlling displayer and the panes themselves.

我倾向于将 V 窗格和相应的 M 结构放在同一个包中。您还需要一些中央控制显示器来显示每个单独的窗格。C 在中央控制显示器和窗格本身之间共享。

Now, if you need more structure in your project, you could highHymanthe Struts 2framework to move all the C in some actions and have panes (the V) indicate what should be the next M/V to display. Struts is originally developed for web applications, but Struts 2 is now detached from the HttpServlet.

现在,如果你在你的项目需要更多的结构,你可以劫持Struts 2的框架来移动所有的C中的一些行动,并有窗格(在V)表示应该是什么下一个M / V来显示。Struts 最初是为 Web 应用程序开发的,但 Struts 2 现在与 HttpServlet 分离。

I have not tried this myself, but I see some benefits. You could JUnit test screen sequences without actually display screens. Just create panes instances pane and invoke the listening methods with events (mouse click etc...).

我自己没有尝试过,但我看到了一些好处。您可以在不实际显示屏幕的情况下通过 JUnit 测试屏幕序列。只需创建窗格实例窗格并使用事件(鼠标单击等...)调用侦听方法。

Just an idea.

只是一个想法。