我正在使用Mig Layout设置从JPanel继承的面板的布局,但是单元格约束不能像我预期的那样正在尝试添加其中一个组件。我希望组件在彼此之上,一列三行。第三个组件紧挨着第二个组件而不是下一个组。我做错了什么?
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");
编辑:我意识到我将MigLayout行和列参数混淆了,但即使我尝试了这个,我仍然遇到了同样的问题。
roundedPanel.setLayout(new MigLayout("fill, insets " + RoundedPanel.RECOMMENDED_INSET, "[][][]", "[]"));
编辑2:我在MigLayout约束中添加了一些内容,所有内容都显示了我的意图。我不确定最初的问题是什么,以及为什么换包没有帮助。
答案 0 :(得分:1)
列/行约束的数量无关紧要(只要有组件就使用最后一个约束),您必须在某处显式添加包裹:在布局约束中,或在每个单元格约束中: / p>
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),与流量无关。因此我怀疑在你的环境中还有其他问题,最好跟踪它以便将来不会打击你。