【问题标题】:Aligning components in Box对齐 Box 中的组件
【发布时间】:2015-01-17 11:11:21
【问题描述】:

编辑:如果你对这个问题投了反对票,你可以留下评论来解释原因,这将更具建设性。

我得到了这个意想不到的结果……

...使用此代码:

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class TestAlignment extends JFrame {

    // Constructor
    public TestAlignment() {

        super("Test Alignment");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Components to show
        JLabel leftLabel = new JLabel("Left");
        JButton centerButton = new JButton("Middle");
        JLabel rightLabel = new JLabel("Right");

        // Add all to box
        Box box = Box.createVerticalBox();
        box.add(leftLabel);
        box.add(centerButton);
        box.add(rightLabel);
        box.add(Box.createHorizontalStrut(180));

        // Align content in box
        leftLabel.setAlignmentX(LEFT_ALIGNMENT);
        centerButton.setAlignmentX(CENTER_ALIGNMENT);
        rightLabel.setAlignmentX(RIGHT_ALIGNMENT);

        // Add box to frame, and show frame
        box.setOpaque(true);
        setContentPane(box);
        setVisible(true);
    }

    // Main
    public static void main(String[] args) {
        // Create frame in EDT
        SwingUtilities.invokeLater(new Runnable() {         
            @Override public void run() { new TestAlignment(); }
        });
    }
}

我现在明白这对JComponent.setAlignmentX() 的预期效果:此方法告诉组件的哪些边必须对齐(顶部标签最左侧与按钮中心对齐,底部标签最右侧对齐)。

我想了解如何让每个标签按预期直观地对齐(左侧标签在左侧,右侧标签在右侧),标签接触 Box 的垂直边缘?

(我知道如何将每个标签放入嵌入的 Box 中,并使用 Box.createHorizontalGlue() 将其强制到左侧或右侧,但在我看来,对于简单的对齐目的来说太多了。我是寻找更简单的东西)

【问题讨论】:

  • 如果您花一点时间阅读有用的标签弹出窗口,您会意识到这与 box 标签的定义无关。

标签: java swing alignment layout-manager boxlayout


【解决方案1】:

不要以为你可以用 BoxLayout 做到这一点。您的示例确实显示了直观的代码,但它并不像您希望的那样工作。

我建议您可能需要使用 GridBagLayout。我认为它以您想要的方式支持 setAlignmentX(...) 方法。

如果没有,那么可以使用Relative Layout。它使用简单,就像 BoxLayout 一样,并且在您使用时支持您想要的对齐方式:

setAlignment( RelativeLayout.COMPONENT );

【讨论】:

    【解决方案2】:

    您可以使用 BoxLyout:

            // Components to show
        // Left
        JLabel leftLabel = new JLabel("Left");
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS));
        leftPanel.add(leftLabel);
        leftPanel.add(Box.createHorizontalGlue());
    
        // Center
        JButton centerButton = new JButton("Middle");
    
        // Right
        JLabel rightLabel = new JLabel("Right");
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.X_AXIS));
        rightPanel.add(Box.createHorizontalGlue());
        rightPanel.add(rightLabel);
    
        // Add all to box
        Box box = Box.createVerticalBox();
        box.add(leftPanel);
        box.add(centerButton);
        box.add(rightPanel);
        box.add(Box.createHorizontalStrut(180));
    
        // Align content in box
        // leftLabel.setAlignmentX(LEFT_ALIGNMENT);
        centerButton.setAlignmentX(CENTER_ALIGNMENT);
        // rightLabel.setAlignmentX(RIGHT_ALIGNMENT);
    

    【讨论】:

    • 我已经知道使用胶水可以做到这一点。如前所述,我正在寻找更简单的东西,如果它存在的话。无论如何感谢陈的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2011-03-03
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多