Reputation: 93
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
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
Reputation: 135
Try to add this:
frame.getContentPane().setOpaque(true);
Upvotes: 0