Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color on a Java panel?

Right now, the background I get is a grey. I want to change it to black. I tried doing something like setBackground(color.BLACK); but it didnt work. Any suggestions?

public test() 
{
    setTitle("Adjustment Form");
    setSize(670,450);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    setLayout(new GridLayout(4,6,2,2));
    setVisible(true);   
}
like image 435
razshan Avatar asked Nov 18 '10 21:11

razshan


People also ask

How do you add color to GUI?

Go to Color Setting -> Colors in System and select one of setting or the one you created at previous step. The selected color is now set for the current system you are logging on. Shut down all GUI screens including SAP GUI pad and restart SAP GUI, and log on the system you set up your own color to.

How do you make a transparent background in Java?

Methods : setBackground(Color c) : method to set the background color to color c. color(int r, int g, int b, int alpha) : creates a new color with specified red, green, blue and alpha value. where alpha is the value of translucency where 255 is opaque and 0 is transparent .


2 Answers

You could call:


getContentPane().setBackground(Color.black);

Or add a JPanel to the JFrame your using. Then add your components to the JPanel. This will allow you to call


setBackground(Color.black);

on the JPanel to set the background color.

like image 188
user489041 Avatar answered Sep 19 '22 20:09

user489041


I think what he is trying to say is to use the getContentPane().setBackground(Color.the_Color_you_want_here)

but if u want to set the color to any other then the JFrame, you use the object.setBackground(Color.the_Color_you_want_here)

Eg:

jPanel.setbackground(Color.BLUE)
like image 27
yormen Avatar answered Sep 18 '22 20:09

yormen