[英]How can I make a button exactly the same size of its text?
Is it possible to make a JButton
take exactly the size of its text? 是否有可能使JButton
完全符合其文本的大小? Since by default, a JButton
will have a small amount of padding (both horizontal and vertical) around its text. 因为默认情况下, JButton
在其文本周围会有少量填充(水平和垂直)。 I would like to remove that padding. 我想删除那个填充。
JButton
has a border by default, you can remove it: JButton
默认有边框,你可以删除它:
button.setBorder(null);
If you want to keep the border, reduce the margin (see Eugenes answer ) 如果你想保持边界,减少边际(见Eugenes答案 )
Clear out the margins in the button with setMargin
. 使用setMargin
清除按钮中的边距。
Removing the border has the side-effect of removing the border. 删除边框具有删除边框的副作用。 If you only want to clear out the margins, use the setMargin
method to set the margins all to zero. 如果您只想清除边距,请使用setMargin
方法将边距全部设置为零。
button.setMargin(new Insets(0,0,0,0));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.