我试图修改我的JButton的背景颜色(和大小),但它不起作用(前景是好的)
public class boutondesign extends JButton implements MouseListener {
private String name;
public boutondesign(String nom){
super(nom);
this.name = nom;
this.setSize(100, 100);
this.addMouseListener(this);
this.setBackground(Color.BLACK);
this.setForeground(Color.white);
在此先感谢您的回答
遵循第一个建议:
public boutondesign(String nom){
super(nom);
this.name = nom;
this.addMouseListener(this);
this.setForeground(Color.white);
this.setBackground(Color.BLACK);
this.setContentAreaFilled(false);
this.setOpaque(false);
this.setBorderPainted(false);
this.setFocusPainted(false);
}
也没有工作
尝试重写paintComponent:
public boutondesign(String nom){
super(nom);
this.name = nom;
this.addMouseListener(this);
this.setForeground(Color.white);
this.setOpaque(false);
this.setBorderPainted(false);
this.setFocusPainted(false);
}
public void paintComponen(Graphics g){
g.setColor(Color.BLACK);
}
不工作:((我也试过g.setColor(getBackground()。setColor(Color.Black))
根据user3437460的评论,我查看了Mac和JButton的细节,
有代码工作
public boutondesign(String nom){
super(nom);
this.name = nom;
this.addMouseListener(this);
this.setForeground(Color.white);
this.setBackground(Color.black);
this.setOpaque(true);
this.setBorderPainted(false);
this.setFocusPainted(false);
}
似乎在Mac上,Opaque属性需要为true,而不会覆盖paintComponents和FocusPainted on“False”
感谢你的帮助