0

I'm setting the layout of a panel that inherits from JPanel using Mig Layout, but the cell constraints aren't working like I expected for one of the components I'm trying to add. I want the components to be on top of each other, one column and three rows. The third component ends up right next to the second one instead of in the following row. What am I doing wrong?

roundedPanel = new RoundedPanel();      //inherits from JPanel
registerPanel = createRegisterPanel();  //returns a JPanel
lblIcon = new JLabel();

setupLicenseInfoLabel();  //sets text of and initializes registerLabel

roundedPanel.setLayout(new MigLayout("fill, insets " + RoundedPanel.RECOMMENDED_INSET, "[]", "[][][]"));

roundedPanel.add(lblIcon, "cell 0 0");
roundedPanel.add(licenseInfoLabel, "cell 0 1");
roundedPanel.add(registerPanel, "cell 0 2");

edit: I realized that I had the MigLayout row and column arguments mixed up, but even when I tried this, I still had the same problem.

roundedPanel.setLayout(new MigLayout("fill, insets " + RoundedPanel.RECOMMENDED_INSET, "[][][]", "[]"));

edit 2 : I added flowy to the MigLayout constraints, and things all displayed how I intended. I'm not sure what the original problem was and why wrap didn't help.

4

1 回答 1

1

列/行约束的数量无关紧要(只要有组件就使用最后一个约束),您必须在某处显式添加包装:在布局约束或每个单元格约束中:

new MigLayout("wrap 1", ...)
// or 
panel.add(someComponent, "wrap, ...")

更新

以某种方式/在哪里包装的替代方法(尽管不寻常)是像您一样将组件放置在网格中(以某种方式错过了,抱歉)。它应该可以工作(并且在下面的代码片段中)而不需要其他任何东西,组件在第一列中放置在另一个下方:

MigLayout layout = new MigLayout();
JComponent content = new JPanel(layout);
content.add(new JTextField("first", 20), "cell 0 0");
content.add(new JTextField("second", 10), "cell 0 1");
content.add(new JTextField("third", 13), "cell 0 2");

另一方面,设置 flowy 布局属性不应该有任何影响:单元坐标始终为 (x, y),与流无关。因此,我怀疑您的上下文中还有其他问题,最好对其进行追踪,以免将来对您造成影响。

于 2013-09-13T15:57:20.087 回答