简体   繁体   中英

Jframe Not Setting Background Color

I have tried a lot of different things to get my project to display background color. It still won't change the color. I know it's something little that just needs to be fixed but I can't find it. Any help is appreciated. (I do have the imports at the top not sure if Stack shows that) -Liam

import java.awt.Color; //importing things that the code needs 
import java.awt.Graphics; //importing things that the code needs 
import javax.swing.JFrame; //importing things that the code needs 





public class bigJavaClass extends JFrame{  //Jframe is the viewer so we can see what we are drawing 
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final int R = 200;
    public static final int G = 200;
    public static final int B = 200;

public static void main(String[] args) {
    bigJavaClass test = new bigJavaClass();
    test.setSize(WIDTH, HEIGHT);
    test.setResizable(false);
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.setVisible(true);
    test.setBackground(new Color (248, 177, 149)); //background color R, G, B
}

    public void paint(Graphics g){
        boolean a = false; //for the variable a 
        boolean b = false; //for the variable b
        boolean c = false; //for the variable c 
        boolean d = false; //for the variable d 
  1. class names should NOT start with a lower case character.

  2. Don't override paint() on a JFrame. You have lost the default painting behaviour, which is to paint the background and components added to the frame.

  3. The "content pane" is painted on top of the frame. So you will need to change the background of the content pane. See: Using Top Level Containers for more information about the structure of the frame. Download working examples from the tutorial and customize to learn how to better structure your code.

Set background before making the frame visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM