ninesalt
ninesalt

Reputation: 4354

Why cant I change the color of a JButton?

I'm trying to change the color of a JButton and after some googling I found that button.setForeground(Color a) should do this, but for some reason it doesn't work. The button color isn't changing.

This is my code:

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();
    }

}

I also tried setBackground(Color a) but this just changed the background of the actual button, not the color inside it.

What am I missing?

Upvotes: 4

Views: 4336

Answers (1)

Anand Dwivedi
Anand Dwivedi

Reputation: 1500

Check out JButton documentation:http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html and you can change the Background color Using :

btn.setBackground(Color.BLACK);//Black By Default
btn.setForeground(Color.GRAY);//Set as a Gray Colour 

Upvotes: 1

Related Questions