Box在对齐组件 [英] Aligning components in Box

查看:720
本文介绍了Box在对齐组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:如果你downvote这个问题,你可以发表评论解释为什么,这将是更有建设性的

我得到这个意外的结果...

I obtain this unexpected result...

...使用此code:

... using this code:

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():这个方法告诉哪一个组件两侧必须对齐(顶部标签最左侧有按钮居​​中对齐和底部的标签最右侧)。

I understand now this works as expected for JComponent.setAlignmentX(): this method tells which sides of the components must be aligned (top label leftmost side aligned with button center and bottom label rightmost side).

我想了解如何有对准每个标签如预期的直观的(上左,上右右标签左侧的标签),标签触摸框的垂直边缘?

I would like to understand how to have each label aligned as expected intuitively (left label on the left, right label on the right), labels touching the vertical edges of the Box?

(我知道该怎么做把每个标签嵌入在一个盒子里,并使用 Box.createHorizo​​ntalGlue()将其强制向左或右侧,但在我看来,太多调整的目的很简单,我在寻找一些更简单的)

(I know how to do with putting each label in a Box embedded, and using Box.createHorizontalGlue() to force it to the left or the right side, but seems to me too much for the simple purpose of alignment. I'm looking for something more simple)

推荐答案

别以为你可以用一个BoxLayout的做到这一点。你的榜样确实显示了直观code,它不工作,你会希望。

Don't think you can do this with a BoxLayout. Your example does show the intuitive code, which doesn't work as you would hope.

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

I would suggest you probably need to use a GridBagLayout. I think it supports the setAlignmentX(...) method the way you want to use it.

如果不是,则可以使用相对布局。它简单易用,喜欢的BoxLayout和不支持你想要的比对,当你使用:

If not, then can use the Relative Layout. It is simple to use, like the BoxLayout and does support the alignment you want when you use:

setAlignment( RelativeLayout.COMPONENT );

这篇关于Box在对齐组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆