How to remove border around buttons?

2019-02-09 01:39发布

问题:

I have a JPanel with the GridLayout. In every cell of the grid I have a button. I see that every button is surrounded by a gray border. I would like to remove these borders. Does anybody know how it can be done?

回答1:

Border emptyBorder = BorderFactory.createEmptyBorder();
yourButton.setBorder(emptyBorder);

For more details on borders see the BorderFactory



回答2:

yourButton.setBorderPainted(false);



回答3:

I think it's very likely the borders are part of the buttons' GUI. You could try calling .setBorder(null) on all the buttons and see what happens!



回答4:

In most recent Java versions it's necessary to call setContentAreaFilled(false) to remove the border entirely. Add an empty border for some padding:

button.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
button.setContentAreaFilled(false);


回答5:

It can be like this:

yourButton.setBorder(null);