更新时间:2023-01-13 15:26:40
我不是GUI制造商的大粉丝:他们通常会自动生成桶装的代码,然后锁定整个开发团队使用一个IDE。此外,此代码通常不可读(检查在Netbeans下使用Matisse时生成的代码)。
我对GUI设计/调试的建议是:
main
方法,允许其他开发人员轻松确定组件的外观。 Action
s ActionListener
s并且每个 JComponent
的 ActionMap
注册这些操作。这允许它们被提取并添加到UI的其他部分(例如 JToolBar
),同时仍然由拥有 JComponent
(即松散耦合)。 TaskManager
类。任何后台处理(在 SwingWorker
s中执行)都将句柄传递给由任务
>任务管理。 (通过调用 setDescription(String)
, setThrowable(Throwable)
, cancel ()
)导致状态栏被更新。它还会导致玻璃窗格显示为全局任务,但这与所有SwingWorkers都解耦/隐藏。观察者
/ 可观察
类,而是更倾向于 ChangeListener
, PropertyChangeListener
或您自己的自定义侦听器实现,用于传播事件。 Observer
传递一个 Object
作为事件,强制客户端代码使用instanceof检查类型并执行downcasts,使代码不可读,使类之间的关系不太清楚。 JTable
超过 JList
,即使在表只有一列的情况下。 JList
在其API中有一些令人讨厌的功能,其中包括您需要提供一个原型值来正确计算其大小。 DefaultTableModel
,因为它通常会导致您将模型数据存储在两个位置:在实际的业务对象中以及2D数组中 DefaultTableModel
坐在上面。相反,简单的子类 AbstractTableModel
- 很容易这样做,意味着您的实现可以简单地委托到数据结构(例如列表
)存储您的数据。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.
Now my question:
How do you plan those user interfaces? What do you do when you need to make changes? How do you debug strange behaviours?!
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.
Does a good and open-source (or freeware) designer for AWT exist? Already looked around and didn't find anything really intelligent.
EDIT: 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?
I've heard about some Eclipse Visual designer. Is that one already production-ready?
---
regards
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).
My recommendations for GUI design / debugging would be:
main
method to each panel (or "top-level" component) implementation, allowing other developers to easily determine what a component looks like.Action
s over ActionListener
s 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).assert SwingUtilities.isEventDispatchThread()
.TaskManager
class that is registered with my UI's status bar. Any background processing (performed within SwingWorker
s) 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.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.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.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.