S. Cakici S. Cakici - 5 months ago 47
Java Question

Cannot change the background color of JFrame ContentPane

So I have a snake program in java, works perfectly, however in my Frame class I cannot change the background color of my JFrame's content pane, I use getContentPane().setBackground(Color.DARK_GRAY); but it is not working, any help ?

Here is my Frame class:

package mainpackage;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;

public class Frame extends JFrame {

private static final long serialVersionUID = 1L;

public Frame() {

getContentPane().setBackground(Color.BLACK); \\NOT WORKING !!
setTitle("Snake by Sarp~");

public void init() {
setLayout(new GridLayout(1, 1, 0, 0));

Screen s = new Screen();



public static void main(String[] args) {
new Frame();


setLayout(new GridLayout(1, 1, 0, 0));

With the above layout manager, any component(s) you add to the frame will completely cover the content pane.

Screen s = new Screen();

You may set the background of the content pane, but then you add a component to the content pane. So you will see the color of the Screen component over top of the content pane.

Set the color of your Screen object to be whatever you want:

s.setBackground( Color.BLACK );