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 !!
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Snake by Sarp~");
setResizable(false);
init();
}
public void init() {
setLayout(new GridLayout(1, 1, 0, 0));
Screen s = new Screen();
add(s);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
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();
add(s);
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 );