【问题标题】:BoxLayout ignores subpanel alignmentBoxLayout 忽略子面板对齐
【发布时间】:2014-09-17 06:49:56
【问题描述】:

我有一个带有 BoxLayout 管理器的 JPanel,其中包含子面板。我希望这些子面板中的组件具有左对齐,但它们总是居中显示。 看起来 BoxLayout 正确对齐了直接插入的组件,但当它们位于子面板内时却无法做到这一点。

我已经修改了http://www.java2s.com/Tutorial/Java/0240__Swing/YAxisAlignment.htm 中的示例,因此每个按钮都放置在子面板中,然后使用 BoxLayout 管理器将子面板放置在主面板中:

public class YAxisAlignX {
  private static Container makeIt(String title, float alignment) {
    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    container.setBorder(BorderFactory.createTitledBorder(title));
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    container.setLayout(layout);

    // modified loop. the original version does not create JPanel pan.
    // adds the buttons directly the the JPanel container with the
    // BoxLayout
    for (int i = 0, n = labels.length; i < n; i++) {
      JPanel pan = new JPanel();
      JButton button = new JButton(labels[i]);
      pan.add(button);
      button.setAlignmentX(alignment);
      pan.setAlignmentX(alignment);
      container.add(pan);
    }
    return container;
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container panel1 = makeIt("Left", Component.LEFT_ALIGNMENT);
    Container panel2 = makeIt("Center", Component.CENTER_ALIGNMENT);
    Container panel3 = makeIt("Right", Component.RIGHT_ALIGNMENT);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(panel1);
    contentPane.add(panel2);
    contentPane.add(panel3);

    frame.pack();
    frame.setVisible(true);
  }
}

如果执行此版本,您可以看到按钮都居中,尽管设置了对齐方式。为什么会这样?有什么解决办法吗?在我的例子中,每个子面板都包含几个组件,我不想直接添加到主面板中。

非常感谢。

【问题讨论】:

    标签: java swing alignment layout-manager boxlayout


    【解决方案1】:

    但它们是对齐的!

    首先,setAlignmentX 在 JComponent 级别更改属性,放置这些组件的布局可能会或可能不会使用此信息。例如,BoxLayout 使用它,但 FlowLayoutBorderLayout 不使用。

    在您的情况下,您正在放置一些具有垂直 BoxLayout 的面板,并且您正在以各种方式对齐它们,并且它有效!碰巧的是,面板会自行拉伸以适应整个列,因此实际上对齐不会改变它们的外观。您可以通过在面板周围设置边框来看到这一点:

    pan.setBorder(BorderFactory.createLineBorder(Color.red));
    

    见:

    面板包含按钮或其他任何东西的事实大多是无关紧要的(它只影响面板想要采用的大小,而不是绝对),BoxLayout 对齐面板而不是面板内部的内容,这就是每个面板的布局工作。这就是为什么按钮的对齐不会受到BoxLayout 的影响。

    现在这些按钮是如何决定它的对齐方式的?这取决于它们所处的布局。按钮位于使用默认 LayoutManager FlowLayoutpan 面板内。现在,正如我所说,FlowLayout 不关心alignmentX/Y 属性,所以行:

    button.setAlignmentX(alignment);
    

    什么都做不了。要在 FlowLayout 中对齐,您需要将其 alignment 字段更改为 FlowLayout.setAligment(int) (docs),您也可以在构造函数中这样做,所以如果我们将 pan 声明更改为:

    JPanel pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    

    您还可以让按钮在其面板内左对齐:

    当然,所有列都向左对齐,因为makeIt 的参数float alignment 不会影响FlowLayout 的对齐方式,只是BoxLayout 的对齐方式(这无关紧要)。您可能希望将该参数更改为 int 并使用不同的 FlowLayout 常量调用该函数。

    总而言之,在原始版本中,button.setAlignmentX(alignment); 行是有意义的,因为按钮被直接添加到 container 面板并且 BoxLayout 正确对齐它们:

    但是,一旦您将按钮放在其他面板中,BoxLayout 就会开始对齐面板(这是因为面板在 BoxLayout 内的工作方式,它们被拉伸以填充整个宽度)而不是按钮,并且按钮对齐最多面板布局。这就是它的工作原理,这样我们才能做出一致的嵌套布局。

    我希望这很清楚。

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 1970-01-01
      • 2013-09-15
      • 2019-09-02
      • 2017-05-20
      • 1970-01-01
      • 2014-04-30
      • 2012-11-06
      相关资源
      最近更新 更多