我之前有一个JLabel,我想点击它。我发现这样做最简单的方法是使它成为JButton并使用以下代码。它现在看起来像JLabel
button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);
这正是我想要的,除了文本现在在中间对齐。现在从我能够阅读其他问题和搜索。这应该工作
button.setHorizontalTextPosition( SwingConstants.LEFT );
然而,文本仍然在按钮中间对齐。我有什么想法可以改变这个?
答案 0 :(得分:35)
您需要使用
setHorizontalAlignment(SwingConstants.LEFT)
HorizontalTextPosition指的是文本相对于图标的位置。
答案 1 :(得分:10)
尝试
button.setHorizontalAlignment(SwingConstants.LEFT);