设置背景颜色Java JButton

问题描述 投票:1回答:2

我试图修改我的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))

java macos jbutton setbackground
2个回答
0
投票

这将生成带有白色文本的黑色按钮

this.setForeground(Color.WHITE);
this.setBackground(Color.BLACK);
this.setOpaque(true);
this.setBorderPainted(false);

但它是一个非常粗糙的,这是与基本的JButton相比

enter image description here


0
投票

根据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”

感谢你的帮助

© www.soinside.com 2019 - 2024. All rights reserved.