How to change the background color on a Java panel?
Asked Answered
F

4

13

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);   
}
Fadeless answered 18/11, 2010 at 21:48 Comment(0)
S
25

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.

Sepal answered 18/11, 2010 at 21:50 Comment(2)
I went ahead and did the getContentPane method and it worked.Fadeless
Hi, I want the colour #3ce3b4, how can I do that?Frustration
P
6

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)
Pavyer answered 15/3, 2016 at 12:50 Comment(0)
M
0

setBackground() is the right method to use. Did you repaint after you changed it? If you change it before you make the panel (or its containing frame) visible it should work

Misunderstood answered 18/11, 2010 at 21:52 Comment(3)
there is no frame. i just made a gridbox. and I just add components like buttons and stuff to it.Fadeless
@Faraz How are you displaying it then? You probably want to add the panel to a JFrame, that's normally how they're usedMisunderstood
I can display it without the frame or a panel. I actually made a keypad to do simple subtraction without using any of frame or panel stuff. I just used the gridlayoutFadeless
R
0

I am assuming that we are dealing with a JFrame? The visible portion in the content pane - you have to use jframe.getContentPane().setBackground(...);

Rance answered 18/11, 2010 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.