让 BoxLayout 将组件移动到顶部,同时从左到右堆叠

发布于 2024-08-24 22:30:27 字数 2105 浏览 6 评论 0 原文

我有一个 JPanel,它在 X_AXIS 方向使用 BoxLayout 。我遇到的问题最好用一张图片来展示: 替代文本

正如您所看到的,左侧的 JPanel 已居中而不是顶部对齐。我希望它们都在顶部对齐并从左到右堆叠,如何使用此布局管理器实现此目的?我写的代码如下:

public GameSelectionPanel(){

    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

    setAlignmentY(TOP_ALIGNMENT);

    setBorder(BorderFactory.createLineBorder(Color.black));

    JPanel botSelectionPanel = new JPanel();

    botSelectionPanel.setLayout(new BoxLayout(botSelectionPanel, BoxLayout.Y_AXIS));

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

    JLabel command = new JLabel("Please select your opponent:"); 

    ButtonGroup group = new ButtonGroup();

    JRadioButton button1 = new JRadioButton("hello world");
    JRadioButton button2 = new JRadioButton("hello world");
    JRadioButton button3 = new JRadioButton("hello world");
    JRadioButton button4 = new JRadioButton("hello world");

    group.add(button1);
    group.add(button2);
    group.add(button3);
    group.add(button4);

    botSelectionPanel.add(command);
    botSelectionPanel.add(button1);
    botSelectionPanel.add(button2);
    botSelectionPanel.add(button3);
    botSelectionPanel.add(button4);

    JPanel blindSelectionPanel = new JPanel();
    blindSelectionPanel.setBorder(BorderFactory.createLineBorder(Color.yellow));

    blindSelectionPanel.setLayout(new BoxLayout(blindSelectionPanel, BoxLayout.Y_AXIS));

    JRadioButton button5 = new JRadioButton("hello world");
    JRadioButton button6 = new JRadioButton("hello world");

    ButtonGroup group2 = new ButtonGroup();
    group2.add(button5);
    group2.add(button6);

    JLabel blindStructureQuestion = new JLabel("Please select the blind structure:");

    blindSelectionPanel.add(blindStructureQuestion);
    blindSelectionPanel.add(button5);
    blindSelectionPanel.add(button6);

    add(botSelectionPanel);
    add(blindSelectionPanel);

    setVisible(true);
}

I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem I have is best shown by an image:
alt text

As you can see the JPanel on the left is has been centered rather than aligned at the top. I would like them both to be aligned at the top and stacked left to right, how can I achieve this with this layout manager? The code I have written is as follows:

public GameSelectionPanel(){

    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

    setAlignmentY(TOP_ALIGNMENT);

    setBorder(BorderFactory.createLineBorder(Color.black));

    JPanel botSelectionPanel = new JPanel();

    botSelectionPanel.setLayout(new BoxLayout(botSelectionPanel, BoxLayout.Y_AXIS));

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

    JLabel command = new JLabel("Please select your opponent:"); 

    ButtonGroup group = new ButtonGroup();

    JRadioButton button1 = new JRadioButton("hello world");
    JRadioButton button2 = new JRadioButton("hello world");
    JRadioButton button3 = new JRadioButton("hello world");
    JRadioButton button4 = new JRadioButton("hello world");

    group.add(button1);
    group.add(button2);
    group.add(button3);
    group.add(button4);

    botSelectionPanel.add(command);
    botSelectionPanel.add(button1);
    botSelectionPanel.add(button2);
    botSelectionPanel.add(button3);
    botSelectionPanel.add(button4);

    JPanel blindSelectionPanel = new JPanel();
    blindSelectionPanel.setBorder(BorderFactory.createLineBorder(Color.yellow));

    blindSelectionPanel.setLayout(new BoxLayout(blindSelectionPanel, BoxLayout.Y_AXIS));

    JRadioButton button5 = new JRadioButton("hello world");
    JRadioButton button6 = new JRadioButton("hello world");

    ButtonGroup group2 = new ButtonGroup();
    group2.add(button5);
    group2.add(button6);

    JLabel blindStructureQuestion = new JLabel("Please select the blind structure:");

    blindSelectionPanel.add(blindStructureQuestion);
    blindSelectionPanel.add(button5);
    blindSelectionPanel.add(button6);

    add(botSelectionPanel);
    add(blindSelectionPanel);

    setVisible(true);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2024-08-31 22:30:27

Riduidel 关于在 GameSelectionPanel 本身上设置 setAlignmentY 的说法是正确的,并且GridBagLayout 是一个很好的替代方案。如果您更喜欢使用 BoxLayout,请参阅文章 修复对齐问题讨论了这个问题,建议“由从左到右的 Boxlayout 控制的所有组件通常应该具有相同的 Y 对齐方式。”在您的示例中,添加

botSelectionPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);
blindSelectionPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);

Riduidel is correct about setting setAlignmentY on the GameSelectionPanel itself, and GridBagLayout is an excellent alternative. If you prefer to stick with BoxLayout, the article Fixing Alignment Problems discusses the matter, suggesting "all the components controlled by a left-to-right Boxlayout should generally have the same Y alignment." In your example, add

botSelectionPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);
blindSelectionPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);
九八野马 2024-08-31 22:30:27

好吧,setAlignmentY 方法在这里不起作用,因为它作用于被视为组件的面板。

正如您所猜测的,包含的面板的布局是由您使用的布局管理器定义的。
不幸的是,BoxLayout 不提供您正在查看的功能。

在标准 JDK 中,显然,您的问题选择的布局是 GridBagLayout。虽然一开始很难理解,但它会很快向您展示其在组件排列方面的强大功能。

使用有用的 GBC 类,您的组件可以这样排列:

setLayout(new GridBagLayout(this));

add(botSelectionPanel, new GBC(0,1).setAnchor(TOP));
add(blindSelectionPanel, new GBC(0,2).setAnchor(TOP));

或者我认为是这样;-)

Well, the setAlignmentY method has no effect here, since it acts on the panel considered as a component.

As you have guessed, the layout of contained panels is defined by the layout manager you use.
Unfortunatly, BoxLayout do not provide the kind of feature you're looking at.

in standard JDK, obviously, the layout of choice for your problem is GridBagLayout. Although rather hard to understand at first, it will fast reveal to you its power in components arrangement.

using the useful GBC class, your components could be arranged as such :

setLayout(new GridBagLayout(this));

add(botSelectionPanel, new GBC(0,1).setAnchor(TOP));
add(blindSelectionPanel, new GBC(0,2).setAnchor(TOP));

or I think so ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文