如何在 JPanel 中将所有元素向左对齐?

发布于 2024-08-30 11:15:15 字数 267 浏览 3 评论 0原文

我希望 JPanel 中的所有元素都向左对齐。我尝试按以下方式执行此操作:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setAlignmentX(Component.LEFT_ALIGNMENT);

结果 Java 使用所有元素的左侧作为元素的位置,然后将所有元素放在 JPanel 的中心(而不是左侧部分)。

I would like to have all elements in my JPanel to be aligned to the left. I try to do it in the following way:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setAlignmentX(Component.LEFT_ALIGNMENT);

As a result Java use left side of all elements as a position of the element and then put all elements in the center (not left part) of the JPanel.

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

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

发布评论

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

评论(3

澉约 2024-09-06 11:15:15

我发现将对象放置在左侧的最简单方法是使用 FlowLayout。

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

通常向该面板添加组件会将其放置在左侧

The easiest way I've found to place objects on the left is using FlowLayout.

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

adding a component normally to this panel will place it on the left

北凤男飞 2024-09-06 11:15:15

您应该在要对齐的组件上使用 setAlignmentX(..) ,而不是在包含它们的容器上使用。

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(c1);
panel.add(c2);

c1.setAlignmentX(Component.LEFT_ALIGNMENT);
c2.setAlignmentX(Component.LEFT_ALIGNMENT);

You should use setAlignmentX(..) on components you want to align, not on the container that has them..

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(c1);
panel.add(c2);

c1.setAlignmentX(Component.LEFT_ALIGNMENT);
c2.setAlignmentX(Component.LEFT_ALIGNMENT);
丘比特射中我 2024-09-06 11:15:15

我最喜欢使用的方法是 BorderLayout 方法。以下是组件可能进入的每个位置的五个示例。该示例针对的是组件是按钮的情况。我们将把它添加到 JPanel,第 17 页。该按钮将被称为 b。

//To align it to the left
p.add(b, BorderLayout.WEST);

//To align it to the right
p.add(b, BorderLayout.EAST);

//To align it at the top
p.add(b, BorderLayout.NORTH);

//To align it to the bottom
p.add(b, BorderLayout.SOUTH);

//To align it to the center
p.add(b, BorderLayout.CENTER);

不要忘记通过键入以下内容来导入它:

import java.awt.BorderLayout;

BorderLayout 类中还有其他方法涉及方向等内容,但如果您对此感到好奇,可以对此进行自己的研究。我希望这有帮助!

My favorite method to use would be the BorderLayout method. Here are the five examples with each position the component could go in. The example is for if the component were a button. We will add it to a JPanel, p. The button will be called b.

//To align it to the left
p.add(b, BorderLayout.WEST);

//To align it to the right
p.add(b, BorderLayout.EAST);

//To align it at the top
p.add(b, BorderLayout.NORTH);

//To align it to the bottom
p.add(b, BorderLayout.SOUTH);

//To align it to the center
p.add(b, BorderLayout.CENTER);

Don't forget to import it as well by typing:

import java.awt.BorderLayout;

There are also other methods in the BorderLayout class involving things like orientation, but you can do your own research on that if you curious about that. I hope this helped!

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