【问题标题】:Aligning all panel components java对齐所有面板组件ja​​va
【发布时间】:2011-02-14 17:52:27
【问题描述】:

我在java中使用BoxLayout布局管理器,并对齐了一堆组件:

myLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
myTextBox.setAlignmentX(Component.LEFT_ALIGNMENT);
myButton.setAlignmentX(Component.LEFT_ALIGNMENT);
...

我有很多组件,这似乎是最重要的。有速记方式吗?

我尝试了以下方法,但 setAlignmentX 不是 Component 内部的方法?

for (Component c : personPanel.getComponents()) {
    c.setAlignmentX(Component.LEFT_ALIGNMENT);
}

【问题讨论】:

    标签: java swing user-interface alignment


    【解决方案1】:

    setAlignmentXJComponent 中定义。

    你可以在检查后施放:

    for (Component c : personPanel.getComponents()) {
        if(c instanceof JComponent) {
            ((JComponent)c).setAlignmentX(Component.LEFT_ALIGNMENT);
        }
    }
    

    如果您已经嵌套了组件,则可能需要从中创建一个递归方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-15
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多