-1

i have some code that creates a jframe but and changes the backgroun color but when i run it, it doesn't change the background color. The code is below.

public static void main(String[] args) {
    mainComponent game = new mainComponent();
    JFrame frame = new JFrame(TITLE);

    frame.pack();
    frame.add(game);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setSize(WIDTH, HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.getContentPane().setBackground(Color.RED);
}

Any ideas?

  • This has been answered here: http://stackoverflow.com/questions/1081486/setting-background-color-for-the-jframe – Lev Mar 30 '15 at 19:07
  • The code is ok. When i copy it to my ide it turns the background red (after slight modifications like TITLE, WIDTH, HEIGHT...). What is the exact problem? – chris Mar 30 '15 at 19:19
  • it wont change it just stays grey. no errors or anything – Andrew Neate Mar 30 '15 at 19:25

1 Answers1

0

To turn the background to red i would use the awt color (0,0,0) (red,green,blue)

import javax.swing.JFrame;
import java.awt.Color;
public static void main(String[] args) {
mainComponent game = new mainComponent();
JFrame frame = new JFrame(TITLE);

frame.pack();
frame.add(game);
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().setBackground(new Color(255,0,0,));
}
Samuel Musa
  • 68
  • 2
  • 11