setAlignmentY 未将 BorderLayout 中的 JLabel 居中

发布于 2024-12-27 04:10:42 字数 587 浏览 1 评论 0原文

对于java新手和该网站来说都是全新的。我在 BorderLayout 的中心面板中添加了一个 JLabel。我希望 JLabel 在面板中居中; setAlignmentX 似乎有效,但 setAlignmentY 无效(标签显示在面板顶部)。这是代码:

centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));

JLabel label = new JLabel("This should be centered");
label.setAlignmentX(Component.CENTER_ALIGNMENT);
label.setAlignmentY(Component.CENTER_ALIGNMENT);
centerPanel.add(label);

contentPane.add(centerPanel, BorderLayout.CENTER);

我也尝试过 label.setVerticalAlignment(CENTER);,但无济于事。我在 API 和 Java 教程、本网站以及通过 google 搜索中寻找了答案。谢谢!

new to java and brand new to the site. I have a JLabel added to the center panel of a BorderLayout. I would like the JLabel to be centered in the panel; setAlignmentX appears to work, but setAlignmentY does not (the label appears at the top of the panel). Here is the code:

centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));

JLabel label = new JLabel("This should be centered");
label.setAlignmentX(Component.CENTER_ALIGNMENT);
label.setAlignmentY(Component.CENTER_ALIGNMENT);
centerPanel.add(label);

contentPane.add(centerPanel, BorderLayout.CENTER);

I have also tried label.setVerticalAlignment(CENTER);, to no avail. I've looked for an answer in the API and in the Java Tutorials, on this site, and through a google search. Thanks!

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

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

发布评论

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

评论(2

北城半夏 2025-01-03 04:10:42

您已经很接近了,试试这个:

public static void main(String[] args)
{
    JFrame contentPane = new JFrame();
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new BorderLayout());

    JLabel label = new JLabel("This should be centered");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    centerPanel.add(label, BorderLayout.CENTER);

    contentPane.add(centerPanel, BorderLayout.CENTER);
    contentPane.pack();
    contentPane.setVisible(true);
}

Java GUI 编程的众多乐趣之一。如果我说实话,我宁愿把我的眼睛挖出来。

You were close, try this:

public static void main(String[] args)
{
    JFrame contentPane = new JFrame();
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new BorderLayout());

    JLabel label = new JLabel("This should be centered");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    centerPanel.add(label, BorderLayout.CENTER);

    contentPane.add(centerPanel, BorderLayout.CENTER);
    contentPane.pack();
    contentPane.setVisible(true);
}

One of the many joys of GUI programming in Java. I'd rather poke my eye out if I'm being honest.

瞄了个咪的 2025-01-03 04:10:42

我尝试垂直居中对齐 JButton 但遇到问题,它被拉伸了。经过一番摆弄,我发现这个有效:

JPanel jpTop = new JPanel(new BorderLayout());
jbStop = new JButton("Cancel");
JPanel extraPanel = new JPanel();
extraPanel.setLayout(new BoxLayout(extraPanel, BoxLayout.X_AXIS));
extraPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
extraPanel.add(jbStop);
jpTop .add(extraPanel, BorderLayout.EAST);

当然它也适用于 JLabel。

I tried to vertically center align JButton but I had problem it was stretched. After fiddling I found this works:

JPanel jpTop = new JPanel(new BorderLayout());
jbStop = new JButton("Cancel");
JPanel extraPanel = new JPanel();
extraPanel.setLayout(new BoxLayout(extraPanel, BoxLayout.X_AXIS));
extraPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
extraPanel.add(jbStop);
jpTop .add(extraPanel, BorderLayout.EAST);

Of course it works as well for JLabel.

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