Java AWT/SWT/Swing:如何规划 GUI? [英] Java AWT/SWT/Swing: How to plan a GUI?

查看:19
本文介绍了Java AWT/SWT/Swing:如何规划 GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一些带有小型图形用户界面的应用程序.没什么复杂的,但我遇到了几个问题,即组件不显示或行为不符合预期.

I've already realized some applications with a small graphical user interface. Nothing complex, but I've encountered several problems that components aren't displayed or just not behaving as expected.

现在我的问题:

您如何规划这些用户界面?当您需要进行更改时,您会怎么做?你如何调试奇怪的行为?!

How do you plan those user interfaces? What do you do when you need to make changes? How do you debug strange behaviours?!

这适用于几乎所有类型的 gui 设计.当然,使用 Microsoft 的 Visual Studio,您有很大的优势,因为您几乎可以在设计器中看到您所看到的内容.

This applies for nearly every type of gui-design. Sure, with Microsofts Visual Studio you have a big advantage because you nearly get what you see in the designer.

是否存在优秀的 AWT 开源(或免费软件)设计师?已经环顾四周,没有发现任何真正智能的东西.

Does a good and open-source (or freeware) designer for AWT exist? Already looked around and didn't find anything really intelligent.

到目前为止,我还手动创建了所有 GUI.当然它是更干净的代码,但有时很难找到布局错误.如果 MS 的 Visual Studio 能够创建大致干净的代码,为什么其他的不能?

Until now, I've also created all of my GUIs by hand. Sure it is cleaner code but sometimes it's very hard to find the layouting bugs. If Visual Studio by MS is able to create approximately clean code, why aren't the others?

我听说过一些 Eclipse Visual 设计器.那个已经准备好生产了吗?

I've heard about some Eclipse Visual designer. Is that one already production-ready?

推荐答案

我不是 GUI 构建器的忠实粉丝:他们通常会自动生成大量代码,然后将整个开发团队锁定为使用一个 IDE.另外,这段代码经常不可读(查看Netbeans下使用Matisse时生成的代码).

I'm not a big fan of GUI builders: They typically autogenerate bucket-loads of code that then locks in your whole development team to using one IDE. Also, this code is often unreadable (check the code generated when using Matisse under Netbeans).

我对 GUI 设计/调试的建议是:

My recommendations for GUI design / debugging would be:

  • 为每个面板(或顶级"组件)实现添加一个 main 方法,允许其他开发人员轻松确定组件的外观.
  • 倾向于使用 Action 而不是 ActionListener 并将这些动作注册到每个 JComponentActionMap.这允许它们被提取"并添加到 UI 的其他部分(例如 JToolBar),同时它们的状态仍然由拥有"JComponent 控制(即松散耦合).
  • 使用 assert 确保所有 UI 组件修改都发生在 Event Dispatch 线程上;例如assert SwingUtilities.isEventDispatchThread().
  • 要调试奇怪的布局行为,请考虑将组件的背景绘制为红色!
  • 集中捕获和报告工作流事件和异常.例如,我通常实现一个 TaskManager 类,该类注册到我的 UI 状态栏.任何后台处理(在SwingWorkers 内执行)都被传递到由TaskManager 创建的Task 的句柄.与任务交互(通过调用 setDescription(String)setThrowable(Throwable)cancel())会导致更新状态栏.它还导致为全局"任务显示玻璃窗格……但这与单个 SwingWorkers 完全分离/隐藏.
  • 不要使用 Observer/Observable 类,而是使用 ChangeListenerPropertyChangeListener 或您自己的自定义用于传播事件的侦听器实现.Observer 传递一个 Object 作为它的事件,强制客户端代码使用 instanceof 检查类型并执行向下转换,使代码不可读并使类之间的关系不太清楚.
  • 赞成使用 JTable 而不是 JList,即使在您的表只有一列的情况下.JList 在其 API 中有一些令人讨厌的功能,包括您需要为其提供原型值才能正确计算其大小.
  • 切勿使用 DefaultTableModel,因为它通常会导致您将模型"数据存储在两个位置:在您的实际业务对象中以及 DefaultTableModel 所在的二维数组中在.相反,只需将 AbstractTableModel 子类化 - 这样做非常容易,这意味着您的实现可以简单地委托给数据结构(例如 List)存储您的数据.
  • Add a main method to each panel (or "top-level" component) implementation, allowing other developers to easily determine what a component looks like.
  • Favour the use of Actions over ActionListeners and register these actions with each JComponent's ActionMap. This allows them to be "extracted" and added to other parts of the UI (e.g. JToolBar) whilst still having their state controlled by the "owning" JComponent (i.e. loose coupling).
  • Use assert to ensure that all UI component modifications are occurring on the Event Dispatch thread; e.g. assert SwingUtilities.isEventDispatchThread().
  • To debug strange layout behaviour consider painting a component's background in red!
  • Centralise the capturing and reporting of workflow events and exceptions. For example, I typically implement a TaskManager class that is registered with my UI's status bar. Any background processing (performed within SwingWorkers) is passed a handle to a Task created by the TaskManager. Interracting with the Task (by calling setDescription(String), setThrowable(Throwable), cancel()) causes the status bar to be updated. It also causes the glass pane to be displayed for "global" tasks ... but this is all decoupled / hidden from the individual SwingWorkers.
  • Do not use the Observer / Observable classes, but instead favour ChangeListener, PropertyChangeListener or your own custom listener implementation for propagating events. Observer passes an Object as it's event, forcing client code to check the type using instanceof and to perform downcasts, making code unreadable and making relationships between classes less clear.
  • Favour the use of JTable over JList, even in situations where your table only has one column. JList has some nasty features in its API including the fact that you need to provide a prototype value for it to calculate its size correctly.
  • Never use DefaultTableModel as it typically results in you storing your "model" data in two places: In your actual business objects and also within the 2D array that DefaultTableModel sits on. Instead, simply subclass AbstractTableModel - It's very easy to do this and means your implementation can simply delegate through to the data structure (e.g. List) storing your data.

这篇关于Java AWT/SWT/Swing:如何规划 GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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