Which Swing layout(s) do you recommend? [closed]

走远了吗. 提交于 2019-11-28 15:39:36

MiGLayout, no doubt. Honestly, it's the only Swing layout manager I know of that makes any sense.

The mere fact that there are 8 layout managers in the core JDK is a good sign that the Swing creators had absolutely no idea about what they were trying to do. This is not to trash the rest of the Swing - it's a good GUI toolkit, except for the layout managers.

All of them, in combination. That's the whole point. Each layout manager fulfills different requirements, and by nesting panels with different layout managers, you can achieve almost anything.

The "do everything in a single panel" layout managers like GridBagLayout and GroupLayout (and lots of 3rd party ones) have their place, mainly when you need components in different parts of the layout to align, but with a large layout, they generally result in a huge, hard-to-handle mess.

It depends on what kind of GUI you are creating. You might use just one or two of the simple layouts, or you might need to reach for a more advanced layout. My overall layout manager use would probably break down to something like this, although it would vary based on the project:

  • 65% GridBagLayout - The one layout that will get it done, no matter what you need to do.
  • 15% Box/BoxLayout - Great for quickly & easily sticking a couple components together.
  • 12% BorderLayout - Good for attaching a button panel or info panel to a content panel. I almost always use it to add content to a JFrame.
  • 3% FlowLayout - Useful for button panels, but not much else.
  • 3% CardLayout - Mostly useful in programs that display different content panels for different operational modes.
  • 2% Other layouts - It's very rare that I need anything else, but occasionally one of the other layouts comes in handy.

Once you get the hang of GridBagLayout, it's not that bad to write initially, but it's still not pretty to work with, or to debug later on. I tried MiGLayout for something recently and was disappointed to find that the MiGLayout actually ended up being more complicated to use than the GridBagLayout in that particular case.

Some people try to avoid GridBagLayout like the plague; but the truth is, there are some things that no combination of simple layouts will be able to handle. It's fine to split a GUI into panels for different logical sections, but I think if you're creating a whole bunch of unnecessary extra nested panels just for the purpose of positioning components, you clearly need to learn how to use a GridBagLayout (or other similarly advanced layout, like MiGLayout). You might get your GUI to look okay with a nasty mess of nested BorderLayouts and GridLayouts and BoxLayouts, but as soon as someone starts resizing your application windows and dialogs to be either smaller or larger than you originally designed them, your GUI will probably look horrible and your customers will start to form a negative opinion about your product since you couldn't get such a simple thing right.

Update: I've been using WindowBuilder in Eclipse for a while now, and it greatly simplifies working with many layouts, especially GridBagLayout. I used to spend a lot of time writing layouts by hand, but with WindowBuilder or probably any similarly advanced visual editor, you can create the layouts in much less time.

DesignGridLayout both looks great and is easy to use through fluent interface.

just look at example:

with just a few lines of clean code:

    layout.row().grid(label("Last Name"))   .add(lastNameField) .grid(label("First Name"))  .add(firstNameField);
    layout.row().grid(label("Phone"))       .add(phoneField)    .grid(label("Email"))       .add(emailField);
    layout.row().grid(label("Address 1"))   .add(address1Field);
    layout.row().grid(label("Address 2"))   .add(address2Field);
    layout.row().grid(label("City"), 1)     .add(cityField);
    layout.row().grid(label("State"))       .add(stateField)    .grid(label("Postal Code")) .add(postalField);
    layout.row().grid(label("Country"), 1)  .add(countryField);
    layout.emptyRow();
    layout.row().center().add(newButton).add(deleteButton).add(editButton).add(saveButton).add(cancelButton);

It depends which kind of layout you need, that's why you have 8 of them:

  1. BorderLayout, simple and quite useful to design normal content frames (with main content in the middle and helpers on sides).
  2. GridLayout, useful when you have a grid of objects that should be of the same size (buttons? textfields?).
  3. GridBagLayout, very flexible, needs some tweaking to be fine and it is quite verbose (but I raccomend it if you want to do things well).
  4. FlowLayout, useless.. not a very layout: just placing item one next to another.
  5. CardLayout, useful for tabs or subviews that must be switched.
  6. BoxLayout, never used it too much.. it should be a sort of enhanced FlowLayout but it's not enough flexible to be used intensively.

GroupLayout is pretty decent. It was originally intended for use by GUI Builder applications but I've found it to be very straight forward to code by hand too.

FormLayout, part of the JGoodies Forms package has been a workhorse for me. It's not perfectly flexible in that it works hard to make your design look good. I've used it for several years on dozens of projects and it keeps on producing good looking output quickly.

You specify your layout in human-readable text, then add the components. Done.

GridBagLayout. Does pretty much all that you need (kind of) and it's in the Java library. Admittedly it does need some help, and the API is terrible.

GroupLayout makes a real mess of your layout code. Okay, so most people's GUI code is a big ball of mud. But your's does not have to be! Perhaps a nice interface could be put onto this layout manager, but I suspect it might have to be cloned-and-owned.

I am not in favour of introducing external dependencies unless they are really necessary. Also a lot of the third-party layout managers use strings of data, which have all the usual issues.

baybora.oren

I usually use border layout with gridlayout, first i design ui on paper prototype like ;


(source: usernomics.com)

After that we can split screen to gridlayout on borderlayout. In this picture we see NORTH, CENTER, SOUTH part (BorderLayout elements) and every part's layout can be gridlayout or BorderLayout, it depends on you. Same Layouts can use one within the other.

Use IntelliJ IDEA with its GUI designer. Makes GridBagLayout easy.

http://madbean.com/anim/totallygridbag/

There is no real answer to your question except that: it depends. It depends on what kind of frame (form) you are trying to create. I am no Swing-guru, but created a couple of (moderately advanced) GUI's and have never had the need to touch the GridBagLayout manager. I have always been able to create my GUI's using a combination of "easier" layout managers. For example, you can give your frame the BorderLayout and then place another layout in the SOUTH of that BorderLayout.

jerome

I only use GridBagLayout, especially when doing some complex interfacing, believe me GridBagLayout can do anything. I also recommended the FlowLayout, because it is easy to use, and understandable, good for putting a group of buttons. Actually I only use this two layouts since it is part of the JDK library as for the MigLayout I haven't tried it yet so for now can't recommend it for you.

There are following layout options available:

MiGLayout
TableLayout
GroupLayout
FormsLayout

Out of these above MigLayout is the most recommended one as it is swing layout manager. There others have not got much exposure.

I'm a Swing neophyte, but in the course of writing my first Swing application, I've tried four different layout managers: FlowLayout, BoxLayout, GridLayout and GroupLayout. In my opinion, FlowLayout and BoxLayout seems most appropriate for laying out groups of components of similar sizes. For components of varying sizes, GroupLayout seems to be the way to go. Steeper learning curve than the other two, but definitely worth it. As for GridLayout, I'd argue that you can achieve the same results you would with that layout manager by using a combination of FlowLayout and BoxLayout -- and you'd probably have more control of the placement of your components. But maybe that's just me :)

Sheldon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!