How to fill the surface of the JButton completely with an ImageIcon?

主宰稳场 提交于 2019-12-01 10:44:09

You need to set the border to EmptyBorder so there is no padding within your button.

add this:

button1.setBorder(BorderFactory.createEmptyBorder());

my result:

Depending on what you ultimately want to achieve you could, set the contentAreaFilled property...

button1.setForeground(Color.RED);
button1.setFocusPainted(true);
button1.setContentAreaFilled(false);
button1.setIcon(new ImageIcon(image));

(note, the frame's content area is yellow to highlight the button)

And/or adjust the margins properties...

button1.setForeground(Color.RED);
button1.setFocusPainted(true);
button1.setContentAreaFilled(false);
button1.setMargin(new Insets(0, 0, 0, 0));
button1.setIcon(new ImageIcon(image));

Just as some additional ideas...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!