Skip to content
Advertisement

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);


        }   


}

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement