简体   繁体   中英

Why won't the background of my JFrame become black?

I'm working on a school project and I'm having trouble changing the background colour. For some reason it keeps showing up as the default grey colour instead of black.

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Class;
import java.lang.reflect.*;


//creates PoolTable claa
public class PoolTable extends JPanel implements Runnable, KeyListener, MouseListener, MouseMotionListener
{
    //creates size of the play screen.

    public JFrame myFrame = new JFrame("Pool!");
    private JPanel panel = new JPanel();
    private int gameWidth;
    private int gameHeight;


        public PoolTable()
        {  
            int width = 750;
            int height = 500;    
            myFrame.setTitle("Pool");
            myFrame.getContentPane().setBackground(Color.BLACK);
            myFrame.setSize(width, height);
            myFrame.setResizable(false);
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setContentPane(this);
            myFrame.setVisible(true);


        }   


}

You are replacing default content pane with yours one. so change background color after replacing content pane.

        myFrame.setContentPane(this);
        myFrame.getContentPane().setBackground(Color.BLACK);

OR do

        this.setBackground(Color.BLACK);

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