Why can not I change the color of JButton?

I am 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 does not work. The color of the button does not change.

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 that just changed the background of the actual button, not the color inside it.

What am I missing?

+4
source share
1 answer

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

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

All Articles