8

Is it possible to make a JButton take exactly the size of its text? Since by default, a JButton will have a small amount of padding (both horizontal and vertical) around its text. I would like to remove that padding.

4

2 回答 2

12

Clear out the margins in the button with 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.

button.setMargin(new Insets(0,0,0,0));
于 2010-04-26T13:33:20.700 回答
11

JButton has a border by default, you can remove it:

button.setBorder(null);

If you want to keep the border, reduce the margin (see Eugenes answer)

于 2010-04-26T10:28:20.793 回答