开发者

How to change the background color on a Java panel?

开发者 https://www.devze.com 2023-01-26 07:54 出处:网络
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?

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() 
{
 开发者_Go百科   setTitle("Adjustment Form");
    setSize(670,450);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    setLayout(new GridLayout(4,6,2,2));
    setVisible(true);   
}


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.


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)


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消