如何构建Swing应用程序 [英] How To Structure a Swing Application

查看:116
本文介绍了如何构建Swing应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对摇摆或GUI设计一般都没有多少经验(大学里的一些WPF应用程序的高度很高),但是我的任务是重构摇摆遗留应用程序的一部分在工作中。



我被要求重构的部分围绕一个弹出窗口,该窗口可以根据某个值对象以三种不同的格式显示。这3种不同的格式都共享一些基本字段,然后有条件地确定其他格式。负责这个GUI元素的类长约5k,我认为它应该分成三个子类,基类中的共享内容都可以扩展。但是我完全不知道这是否是正确的方法。



任何人都可以强调一些用于处理不同摆动组件的策略,这些组件共享按钮/字段等元素等等?



此外,是否有可用于学习的大型OSS挥杆应用程序?



更多信息:我正在处理的应用程序是一个庞大的遗留应用程序,目前非常可怕。我是团队的新手(也是一个相当近的毕业生,因此在这方面没有太多的经验)并且被要求尝试打破其中一个负责显示此弹出窗口的巨大课程更小的可维护组件。实际上,应用程序中有一个弹出窗口允许用户响应某些事件,这有三种不同的外观,具体取决于他们需要响应的请求的子类型。很大一部分GUI元素在所有三种子类型中是一致的,因此我有兴趣知道继承是否是这里最好的方法还是有其他策略来处理这个?

解决方案

阅读评论,我想我可以回答这个问题。一个真正的答案需要一本书。


  1. 将你的GUI分解为多个嵌套的 JPanel s。使用 BorderLayout 的简单嵌套 JPanel 优于复杂的 JPanel 使用 GridBagLayout 。要清楚,我不批评 GridBagLayout 。它在创建表单时很有用。但它不是唯一的Swing布局管理器。


  2. 将每个嵌套的 JPanel 放入它自己的类中。 / p>


  3. 使用Swing组件时使用合成。当且仅当您的类将覆盖 JComponent 方法之一时才使用继承。


  4. 每个 JPanel 有自己的 JButton JLabel 等组件。 JBUtton A定义为 JPanel A, JButton B定义为 JPanel B,即使GUI的用户认为它们是相同的按钮。您可以通过创建包含标签和按钮文本的GUI模型来最小化重复。您必须通过编写公共 ActionListener 来消除动作代码的重复(按下按钮时执行的代码) JButton A和 JButton B可以执行。


  5. Swing应用程序必须首先调用 SwingUtilities.invokelater()。这可以确保在事件调度线程(EDT)上定义和使用Swing组件。


  6. 一个 JFrame 用于Swing应用程序。


  7. 每个JPanel 必须具有Swing布局管理器定义。


  8. 某些组件,例如 JList JTable ,当包含在 JScrollPane 中时效果更好。




<我确信我已经忘记了一些事情,但这应该是一个好的开始。


I don't really have much experience with swing, or GUI design in general for that matter (a few WPF applications in university is about the height of it), however I have been tasked with refactoring part of a swing legacy application at work.

The part I've been asked to refactor revolves around a popup window which can display in three different formats depending on a certain value object. These 3 different formats all share a few base fields, and then have additional ones determined conditionally. The class responsible for this GUI element is ~5k long and I was thinking it should be split into three subclasses, with the shared stuff in base class which they all extend. However I have absolutely no idea if this is the correct approach or not.

Could anyone highlight some strategies used in dealing with different swing components which share elements such as buttons/fields etc?

Additionally, are there any large OSS swing applications which can be used to learn from?

More info: The application I am working on is a large legacy application which is rather horrifically structured at the moment. I'm new to the team (and a fairly recent grad so don't have much experience in this area) and have been asked to try and break down one of the huge classes which is responsible for the display of this popup at the moment into smaller more maintainable components. Essentially there is a pop up in the application which allows the user to respond to certain events, and this has three different appearances depending on the sub type of the request they need to respond to. A large portion of the GUI elements are consistent across all three sub types, as such I am interested to know if inheritance would be the best approach here or are there other strategies for dealing with this?

解决方案

Reading the comments, I think I can answer this question. A true answer would require a book.

  1. Break up your GUI into as many nested JPanels as it takes to describe your GUI. A simple nested JPanel that uses a BorderLayout is preferable to a complicated JPanel that uses a GridBagLayout. To be clear, I'm not criticizing a GridBagLayout. It's useful when creating a form. But it's not the only Swing layout manager.

  2. Put each nested JPanel into its own class.

  3. Use composition when using Swing components. Use inheritance if and only if your class will override one of the JComponent methods.

  4. Each JPanel has its own JButton, JLabel, etc. components. JBUtton A is defined for JPanel A, and JButton B is defined for JPanel B, even if the user of the GUI thinks they are the same button. You can minimize the duplication by creating a GUI model that contains the text of the labels and buttons. You must eliminate the duplication of action code (the code that's executed when the button is pressed) by writing common ActionListeners that JButton A and JButton B can execute.

  5. A Swing application must start with a call to SwingUtilities.invokelater(). This ensures that the Swing components are defined and used on the Event Dispatch thread (EDT).

  6. Only one JFrame is used in a Swing application.

  7. Each JPanel must have a Swing layout manager defined.

  8. Certain components, like JList and JTable, work better when enclosed in a JScrollPane.

I'm sure I've forgotten a few things, but this should be a good start.

这篇关于如何构建Swing应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆