John Doe
John Doe

Reputation: 93

setBackgroundColor is not working

I'm trying to set JFrame frame a background color, but it is not working. What am I missing here? this is the code:

public class PingPong extends JPanel{

private static final long serialVersionUID = 1L;
Ball ball = new Ball(this); 
Table table = new Table(this);
Player player = new Player(this);
PC pc = new PC(this);

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);

    table.paint(g);
    ball.repaint(g);
    player.repaint(g);
    pc.repaint(g);

}

public static void main(String[] args){
    /* Creating the frame */
    JFrame frame = new JFrame();
    frame.setTitle("Ping Pong!");
    frame.setSize(600, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new PingPong());
    frame.getContentPane().setBackground(Color.DARK_GRAY);
    frame.setVisible(true); 
}

}

It just won't change colors..

Upvotes: 1

Views: 991

Answers (2)

Drumnbass
Drumnbass

Reputation: 914

Since you add a JPanel (PingPong object) to your JFrame, the JPanel is over the JFrame, so the color of the JFrame becomes not visible.

Apply setBackground(Color.DARK_GRAY); to your PingPong object.

Upvotes: 2

candy.cloud.nimbus
candy.cloud.nimbus

Reputation: 135

Try to add this:

frame.getContentPane().setOpaque(true);

Upvotes: 0

Related Questions