1

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..

Kiki
  • 2,243
  • 5
  • 30
  • 43
John Doe
  • 93
  • 2
  • 8
  • It actually works, Just the color is covered by the PingPong object. – dragon66 May 14 '15 at 10:52
  • Have a look at this answer: http://stackoverflow.com/questions/9029367/java-jframe-background-color-not-working Seems to be your problem. – LarsBauer May 14 '15 at 11:01

2 Answers2

2

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.

Drumnbass
  • 867
  • 1
  • 14
  • 35
0

Try to add this:

frame.getContentPane().setOpaque(true);
Spikatrix
  • 20,225
  • 7
  • 37
  • 83