它坚持向左,但是当我使用gapleft时它确实有效,为什么?
基于这一行:
super.setLayout(new MigLayout()); // why super? Did you override setLayout() method?
默认情况下米格布局 http://www.miglayout.com/rows 不会填充所有可用宽度,而只会填充显示最长行所需的宽度(基于组件宽度)。话虽如此,你的JLabel
适合徽标图像宽度,仅此而已,看起来就像粘在左侧。您必须告诉布局管理器,在实例化它时它必须填充所有可用宽度:
super.setLayout(new MigLayout("fillx"));
Or
LC layoutConstraints = new LC();
layoutConstraints.setFillX(true);
super.setLayout(new MigLayout(layoutConstraints);
然后,您的组件约束将按预期工作。
Picture
基于此代码片段:
MigLayout layout = new MigLayout("fillx, debug");
JPanel content = new JPanel(layout);
JLabel label = new JLabel("Warehouse");
label.setFont(label.getFont().deriveFont(Font.BOLD | Font.ITALIC, 18));
CC componentConstraints = new CC();
componentConstraints.alignX("center").spanX();
content.add(label, componentConstraints);
Note:您可以通过执行以下操作来启用调试功能:
super.setLayout(new MigLayout("fillx, debug"));
Or
LC layoutConstraints = new LC();
layoutConstraints.setFillX(true);
layoutConstraints.setDebugMillis(500);
super.setLayout(new MigLayout(layoutConstraints);