-1

My problem is i want to change the background of my GUI to a different color but when i use JFrame.setBackground(color.RED) it doesnt work any other ideas or solutions?

public static void Home() 
{
    frame.setVisible(false);
    JPanel panel2 = new JPanel();
    JFrame frame2 = new JFrame ();
    frame2.setBackground(Color.RED);
    panel2.setLayout(null);
    frame2.setSize(1500,750);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setVisible(true);
    frame2.add(panel2);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • A simple search using "jframe background color" yields many questions on this topic. Also, don't use a null layout. Swing was designed to be used with layout managers. You can also do a search if you don't know what a layout manager is. – camickr May 19 '23 at 15:54

1 Answers1

0

Set the back ground color in the content pane. Here is a quick demo. Note the setting preferredSize() directly is not always a good idea. Nor is using a null layout. This is for demo purposes.

JPanel panel2 = new JPanel();
JFrame frame2 = new JFrame ();
frame2.setLayout(new FlowLayout());
frame2.getContentPane().setBackground(Color.RED);
frame2.setPreferredSize(new Dimension(1000,700));
panel2.setPreferredSize(new Dimension(600,600));
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(panel2);
frame2.pack();
frame2.setLocationRelativeTo(null); // center frame on screen
frame2.setVisible(true);
WJS
  • 36,363
  • 4
  • 24
  • 39