为什么我不能改变JButton的颜色?



我试图改变JButton的颜色,经过一些谷歌搜索,我发现了这个按钮。setForeground(Color a)应该做到这一点,但由于某种原因它没有工作。按钮颜色没有改变。

这是我的代码:

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;

public class test extends JFrame{
    public test(){
        super();
        setSize(100,100);
        setVisible(true);
        JButton x = new JButton();
        x.setForeground(Color.BLACK);
        add(x);
    }
    public static void main(String[] args) {
        new test();
    }
}

我也试过setBackground(Color a),但这只是改变了实际按钮的背景,而不是里面的颜色

我错过了什么?

查看JButton文档:http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html你可以使用

来改变背景颜色
btn.setBackground(Color.BLACK);//Black By Default
btn.setForeground(Color.GRAY);//Set as a Gray Colour 

最新更新