问题
Consider two JPanel
instances using DesignGridLayout, like the following:
JFrame demoApplicationFrame = new JFrame();
JPanel singleDesignGridLayout1 = new JPanel();
DesignGridLayout layout1 = new DesignGridLayout(singleDesignGridLayout1);
layout1.row().grid(new JLabel("Short lbl1")).add(new JTextField(""));
layout1.row().grid(new JLabel("Short lbl2")).add(new JTextField(""));
JPanel singleDesignGridLayout2 = new JPanel();
DesignGridLayout layout2 = new DesignGridLayout(singleDesignGridLayout2);
layout2.row().grid(new JLabel("A bit longer label"))
.add(new JTextField(""));
layout2.row().grid(new JLabel("Another long label here"))
.add(new JTextField(""));
demoApplicationFrame.getContentPane().add(singleDesignGridLayout1,
BorderLayout.NORTH);
demoApplicationFrame.getContentPane().add(singleDesignGridLayout2,
BorderLayout.SOUTH);
Executing that code, will create this window:
Is it possible to align the column for labels of the two panels?
回答1:
Is it possible to align the column for labels of the two panels?
No it's not because DesignGridLayout works with grids within the same container. So if your components are placed in different panels there's nothing DesignGridLayout
can do to ensure the alignment of cross-panels components.
In this context you can only look for workarounds (if any). Be aware of this: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? Yes, we should. So setting a fixed labels size is not an option either.
Some thoughts
I was thinking about this matter and it's not a DesignGridLayout
limitation but this is about how layout managers work. If you take a look to How Layout Managers work section of Laying Out Components Within a Container lesson, it states this:
Layout managers basically do two things:
- Calculate the minimum/preferred/maximum sizes for a container.
- Lay out the container's children.
Layout managers do this based on the provided constraints, the container's properties (such as insets) and on the children's minimum/preferred/maximum sizes. If a child is itself a container then its own layout manger is used to get its minimum/preferred/maximum sizes and to lay it out.
This means if a child itself is a container then it may have its own layout manager to lay out its own children components. This property makes this container independent of its ancestor (about laying out components). If you had a "global" (just to name it somehow) layout manager that aligns components within two separate containers, then it would be breaking the aforementioned contract because children containers wouldn't be independent of their ancestors.
I'm relatively new to this layout managers stuff but I don't know any layout manager that allows you do what you need (or you feel you need), because they don't have to, actually. IMHO you should reconsider if you really need all those panels or you can manage all your components in a single container. Maybe there's a more suitable layout manager to your needs in this list:
- MiG Layout
- FormLayout
- GridBagLayout (not my favorite at all but it might work)
来源:https://stackoverflow.com/questions/22219544/align-the-label-column-of-different-designgridlayout