如何隐藏包含图标的JButton的背景?

9
我有这段代码用于创建一个带有图标的JButton,并且代码是有效的。但问题在于按钮的边框和背景并没有消失。
我想只让图标显示,而不显示按钮的边框和背景。
我尝试设置setOpaque(false),但所有的按钮都消失了!
我的代码哪里出错了?
_button  = new JButton("Exit");
_button.setHorizontalTextPosition(SwingConstants.CENTER);
_button.setSize(200,130);
//_button.setContentAreaFilled(false);
_button.setBorderPainted(false);
//_button.setOpaque(false);
_button.setIgnoreRepaint(true);
//_button.setFocusable(false);
_button.setIcon(button_icon);
_button.setBounds(200, 200,200, 170);
_button.setRolloverEnabled(true);
_button.setRolloverIcon(button_icon_hover);
_button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//_button.addActionListener(this);
_button.setBackground(null);
_button.setFocusable(true);
_button.setFocusPainted(true);
_button.setForeground(Color.WHITE);
_button.setFont(new Font("Times New Roman",Font.BOLD,35));
2个回答

22
尝试这个。
JButton play = new JButton("This",new ImageIcon("src\play.png"));
        play.setBorderPainted(false); 
        play.setContentAreaFilled(false); 
        play.setFocusPainted(false); 
        play.setOpaque(false);


我使用这段代码,但边框和背景消失了,问题是图像无法点击。 - Mohammad Reza Rezwani
@SanjitKumarMishra 为什么你说“部分地”? - Yanki Twizzy
@YankiTwizzy:它没有完全删除背景,所以我说它是部分删除。可能是我正在使用的LookAndFeel限制了这样做。 - Sanjit Kumar Mishra

0

如果按照以下步骤操作,它应该可以工作:

play.setBorder(null);
play.setContentAreaFilled(false);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接