将 JButton 文本设置为左对齐?

发布于 2025-01-06 17:55:32 字数 449 浏览 5 评论 0原文

我以前有一个 JLabel,我希望它可以点击。我发现执行此操作的最简单方法是将其设为 JButton 并使用以下代码。它现在看起来像一个 JLabel

button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);

,这正是我想要的,只是文本现在在中间对齐。现在,从我能够阅读的其他问题和搜索中。这应该可以工作

button.setHorizontalTextPosition( SwingConstants.LEFT );

但是,文本仍然在按钮的中间对齐。我有什么想法可以改变这个吗?

I previously had a JLabel, that I wanted to be click-able. The easiest way I found to do this was make it a JButton and using the following code. It now looks like a JLabel

button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);

Which is exactly what I wanted except the text is now aligned in the middle. Now from what I was able to read on other questions and searching. This should work

button.setHorizontalTextPosition( SwingConstants.LEFT );

Yet, the text still aligns in the middle of the button. Any ideas what I can do to change this?

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

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

发布评论

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

评论(2

欢你一世 2025-01-13 17:55:32

您需要使用

  setHorizontalAlignment(SwingConstants.LEFT)

Horizo​​ntalTextPosition 来指代文本相对于图标的位置。

You need to use

  setHorizontalAlignment(SwingConstants.LEFT)

HorizontalTextPosition refers to the position of text in relation to the icon.

幻想少年梦 2025-01-13 17:55:32

尝试

button.setHorizontalAlignment(SwingConstants.LEFT);

Try

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