简体   繁体   中英

Best swing's layout solution in java

i'm trying to reproduce a mail client like osx lion's client. So i've to do something like:

osx lion的客户邮件

I don't know what is the best solution of layout to do that.. can someone give me some suggest? thanks!!!

The solution would be to use a number of different layouts and components , nested within each other until you have built up the overall layout you are looking for. Typically you would use JPanels for each of the nested areas, and perhaps a JSplitPane for the resizable window areas.

You'll may find you need to write (or find in a 3rd party library library) a number of custom components for specific features.

At a guess, you could do most of the mail client layout with a combination of BorderLayout and GridBagLayout . But you might also want to consider MigLayout , which is a great general purpose layout manager that is very flexible.

If you haven't done so alredy, you should do the excellent Java Swing Tutorials

PS WindowBuilder is a great tool for quickly prototyping, but for more complex GUI designs like this I think you are ultimately better hand coding them.

Overall for that screenshot, a horizontal box layout might be what you're after (laying out the components from left to right to fill the space.)

You may well need to nest other layouts inside that though, as you would with most reasonably sized UIs.

This is something you'd use JSplitPane for. If you don't want the user to be able to resize the division between left and right you'd use BorderLayout where the left would go work like:

panel = new JPanel( new BorderLayout() );
panel.add( new LeftPanel(), BorderLayout.WEST );
panel.add( new CentralPanel(), BorderLayout.CENTER );

That way the LeftPanel would be sized according its preferred width, and the center would be given the remaining width and height so it would resize itself as the user grew and shrank the window, but he left panel would be fixed width and grow and shrink in the height.

If you're just wondering about general purpose layout TableLayout is by far the simplest vs most flexible. Not many layout problems can't be conquered using it, and its easy to understand (as opposed to SpringLayout, GroupedLayout, etc) and far less code and more robust than GridBagLayout.

http://java.sun.com/products/jfc/tsc/articles/tablelayout/

But for the problem you describe I don't think you have to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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