将 JButton 文本设置为左对齐?
我以前有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
HorizontalTextPosition 来指代文本相对于图标的位置。
You need to use
HorizontalTextPosition refers to the position of text in relation to the icon.
尝试
Try