开发者

Best way to set up Java GUI

I'm currently working on my first GUI Programming project in Java as a school assignment. I've gotten pretty familiar with setting up classes and methods in Java, though with the introduction to GUI programming, I'm not sure how best to go about it.

I've set up the GUI in its own package. Should I make a new cl开发者_开发百科ass for each JPanel? I've enclosed a screenshot for what my GUI looks like so far.

alt text http://www.aaronmoodie.com/gui.png

Any advice on this would be really great.


It depends on what you are trying to achieve. For example, I don't necessarily create a separate class for each JPanel. However if the panel is a custom(Overridden) panel, or a panel that contains a lot of other components, or contains a substantial amount of functionality then I put it in it's own class. I try to separate by functionality, if that makes sense. There is not particular reason I do it this way, other than after writing multiple gui's this seems to make things simpler and a lot cleaner. For instance in your example above, without knowing much about your program. I might create a component with a jPanel, the label, and the list for the components on the first row down, that way I could re-use the component w/ out rewriting the logic over and over.


Javabuilders is my favorite way of doing simple GUIs. You declare your hierarchy of GUI elements + layout info in a YAML file which is much easier than explicitly creating windows manually and adding them to their parent elements.

I generally don't override standard GUI classes. It looks like you have a bunch of JLabels, JButtons, JLists, and JSpinners with some contained in JPanels. You can separate the task of creating all these windows properly in a hierarchy (which doesn't require any custom classes), from the task of managing their behavior, which is pretty easy by adding EventListeners to the particular components you are interested in managing.

The Javabuilders approach is nice because you can name all the components (these are internal names available to you as a programmer, not displayed), let it manage the window creation, and then access any components of interest programmatically by name, rather than by having to traverse the window hierarchy.


Make sure you understand the MVC pattern, specially in context of Swing.

Extending swing container classes (such as JPanel) typically is motivated by adding reusable application semantics to the component.

Beyond that, an extended (top level JPanel) is also a good candidate class for exposing 'control' functionality that manages the nested components. For example, in your gui, the top level container could be an extension class that has methods for managing the state of the nested panels based on user actions. (But note that you do not necessarily have to extend JPanel to manage the state of nested components).


If you are using Eclipse, I quite like the Jigloo GUI builder. It is free for non-commercial use and quite reasonably priced for a commercial license.

I tend to find I use something like Jigloo to visually set up all my components and add stubs for event handlers, then reopen the same file in the standard Java editor to put the code in behind it all. Works well for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜