Java 是否将JButton文本设置为向左对齐?
我以前有一个JLabel,我希望它能够被点击。我发现的最简单的方法是将其设置为JButton并使用以下代码。它现在看起来像一个JLabelJava 是否将JButton文本设置为向左对齐?,java,swing,jbutton,alignment,text-alignment,Java,Swing,Jbutton,Alignment,Text Alignment,我以前有一个JLabel,我希望它能够被点击。我发现的最简单的方法是将其设置为JButton并使用以下代码。它现在看起来像一个JLabel button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); button.setBorderPainted(false); button.setContentAreaFilled(false); button.setFocusPainted(false); 这正是我想要的,除了文本现在在中间
button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);
这正是我想要的,除了文本现在在中间对齐。现在从我能读到的其他问题和搜索。这应该行得通
button.setHorizontalTextPosition( SwingConstants.LEFT );
但是,文本仍然在按钮的中间对齐。我能做些什么来改变这一切
您需要使用 setHorizontalAlignment(SwingConstants.LEFT)
水平文本位置是指文本相对于图标的位置。Try
button.setHorizontalAlignment(SwingConstants.LEFT);