简体   繁体   English

Swing桌面应用程序 - 如何组织我的代码

[英]Swing desktop app - how to organise my code

We are going to use Swing for our next project which will be a lightweight desktop app. 我们将使用Swing作为我们的下一个项目,它将是一个轻量级桌面应用程序。

I've been reading the Swing tutorials on Oracle's website and have started to get the hang of it. 我一直在阅读Oracle网站上的Swing教程,并开始了解它。

I was told that instead of switching JFrame I need to work within one JFrame and switch JPanels . 有人告诉我,我不需要切换JFrame ,而是需要在一个JFrame中工作并切换JPanels

My questions to you would be the following. 我向你提出的问题如下。

Can I create the JPanel designs (or their templates) in design mode (WYSIWYG editor) and call jframe.setContentPane(nameOfJPanel) or do I do them programatically in code? 我可以在设计模式(WYSIWYG编辑器)中创建JPanel设计(或其模板)并调用jframe.setContentPane(nameOfJPanel)或者我是否在代码中以编程方式执行它们?

Secondly, how do I structure my code... If I dynamically add new JPanels and bind their events to an event handler.. all my logic and code will be in one HUGE class with tens of methods. 其次,我如何构建我的代码...如果我动态添加新的JPanel并将它们的事件绑定到事件处理程序..我的所有逻辑和代码都将在一个包含数十种方法的HUGE类中。 I just don't know how to proceed and nowhere can I find an example with multiple (more than 6 let's say) panels on the internet. 我只是不知道如何继续,我无处可以在互联网上找到多个(超过6个让我们说)面板的例子。

To WYSIWYG or not WYSIWYG 到WYSIWYG或WYSIWYG

This is a debatable question at the best of times. 在最好的时候,这是一个值得商榷的问题。

I encourage all my junior developers to start out hand coding UIs as it teaches them important basics about how to use layout managers and how to handle compound layouts. 我鼓励所有初级开发人员开始手工编写UI,因为它教会了他们关于如何使用布局管理器以及如何处理复合布局的重要基础知识。

This tends to take longer as you need to verify the layout with each change. 这需要更长的时间,因为您需要在每次更改时验证布局。

I personally use the form editor in Netbeans for most of my general work, but will hand tweak UI's 我个人使用Netbeans中的表单编辑器来完成我的大部分工作,但是会手动调整UI

Structure 结构体

Think about boundaries of responsibility, reuse and reduce strategies. 考虑责任的界限,重用和减少策略。

What you don't want is some huge master class that does EVERYTHING. 你不想要的是一些能做所有事情的巨大的大师班。 It will be difficult to maintain and update (I live with this horror every day). 维护和更新很困难(我每天都生活在这种恐怖中)。

Instead, identify the distinct areas of responsibility and either use getters and setters or models to move the data around the application. 相反,确定不同的责任区域,并使用getter和setter或模型来移动应用程序周围的数据。 The more you can decouple your code, the easier it will be to update and modify. 您可以越多地解耦代码,更容易更新和修改。

Identify like work and model it as interfaces and abstract classes where you can. 识别工作并将其建模为接口和抽象类。 Basically where ever you start thinking about coping code is probably a good indication that your design is off and you should consider implementing abstract classes to cover the overlap. 基本上,只要你开始考虑应对代码,就可能很好地表明你的设计是关闭的,你应该考虑实现抽象类来覆盖重叠。

Take advantage of the Action API for replicating commonly used concepts (copy and paste is an example of this. You would want menu items, possible toolbar items and maybe even popup items, these can all be handled by the same Action class). 利用Action API来复制常用的概念(复制和粘贴就是一个例子。你可能需要菜单项,可能的工具栏项甚至弹出项,这些都可以由同一个Action类处理)。

Separate the data from view. 将数据与视图分开。 As I said before, take advantage of models. 正如我之前所说,利用模型。 The data shouldn't care how it is collected or modified, only that it can be. 数据不应该关心它是如何被收集或修改的,只能是它可以。 Equally, the view shouldn't care about how the data is managed, only that it is. 同样,视图不应该关心数据的管理方式,只关注数据的管理方式。

If possible, define interfaces between the separate areas of your application. 如果可能,请在应用程序的不同区域之间定义接口。 This way you can further decouple of the application and no one part becomes reliant on any one implementation (hello my world :P) 这样你可以进一步解耦应用程序,没有任何一个部分变得依赖于任何一个实现(你好我的世界:P)

Don't be tempted to simply dig through a component hierarchy to gain access to that field, it will produce a nightmare if you need to change the code!! 不要试图简单地挖掘组件层次结构以获得对该字段的访问权限,如果您需要更改代码,它将产生噩梦!!

The JFrame will be the main window of your app and the panels will be the brick composing it. JFrame将成为您应用的主窗口,面板将成为构成它的砖块。

You should create each panel in its own class and you can arrange them by functionality in packages. 您应该在自己的类中创建每个面板,并且可以通过包中的功能来安排它们。 You'll have panels for holding content of logical part of your application and panels that contains real stuff. 您将拥有用于保存应用程序逻辑部分内容的面板以及包含真实内容的面板。 A logical panel could be the main view and the menu bar. 逻辑面板可以是主视图和菜单栏。 An application panel would be a form, a menu, a canvas... 应用程序面板可以是表单,菜单,画布......

You'll have to put application panels inside logical panel and to change the content of the logical in response to users actions. 您必须将应用程序面板放在逻辑面板中,并根据用户操作更改逻辑内容。

You should study how layouts work in order to compose the view inside the JFrame and to layout components inside panels. 您应该研究布局如何工作,以便在JFrame内部构成视图并在面板内部布局组件。

For example the BorderLayout is a classic when defining the main area of an application: - menu and toolbar on top - browser on the left - status bar at the bottom - main panel on the center 例如,BorderLayout在定义应用程序的主要区域时是经典的: - 顶部的菜单和工具栏 - 左侧的浏览器 - 底部的状态栏 - 中心的主面板

You can use WYSIWYG editor, but avoid doing all design inside the same class. 您可以使用WYSIWYG编辑器,但避免在同一个类中进行所有设计。 Else you'll have an horrible HUGE class. 否则你会有一个可怕的巨大课程。 Create your panels in separate classes and compose them in your main view. 在单独的类中创建面板,并在主视图中组合它们。
You can use empty panels as placeholder to help you create the structure of your application. 您可以使用空面板作为占位符来帮助您创建应用程序的结构。

You'll have to bind your domain data to the view in order to not mix the two layer. 您必须将域数据绑定到视图,以便不混合两个层。 Bind means that you will write a way to go from a java bean to a form and the reverse. 绑定意味着你将编写一种从java bean到表单的方法,反之亦然。 Basic binding is handwritten, but some tools exists to do that. 基本绑定是手写的,但有一些工具可以做到这一点。 If it is a small application, it is maybe better for you to handwrite everything. 如果它是一个小应用程序,你可能更好地手写所有东西。

Building a Swing application can be very tricky (you'll have to know about the event dispatch thread, layout management, event management, widgets (label are trivial, but JTable can be very complicated to handle); 构建Swing应用程序可能非常棘手(您必须了解事件调度线程,布局管理,事件管理,小部件(标签很简单,但JTable处理起来非常复杂);

I'll recommend to find a book about the topic, and to find some open source swing application in order to study how it structured, before you start your project. 在你开始你的项目之前,我建议你找一本关于这个主题的书,找一些开源的swing应用程序,以便研究它的结构。

This is how I organize my code: 这就是我组织代码的方式:

class Panel1 extends JPanel{
   //code for panel1 and its Components
}

class Panel2 extends JPanel{
   //code for panel2 and its components
}
// and it follows. 
class ApplicationFrame extends JFrame{
    /// LOGIC to switch between panels
}

class Main{
  //contains main() function
}

Any HAND-CRAFTED GUI code, is better than a COMPUTER GENERATED WYSIWYG -APP Code. 任何HAND-CRAFTED GUI代码都优于计算机生成的WYSIWYG -APP代码。

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

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