1

I have tried to change the background color of a JFrame and it just never works. Here is my code:

package com.company;

import javax.swing.*;
import java.awt.*;

public class Main extends JPanel
{
    private static JFrame frame = new JFrame();
    private static int rand = 3;

    public static void main(String[] args) 
    {
        frame.getContentPane().add(new Main());
        frame.setSize(1960, 1070);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.BLACK);
        frame.setVisible(true);
        frame.setResizable(false);
    }
}
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Laser Infinite
  • 253
  • 1
  • 15

2 Answers2

1

You'll need to go through getContentPane to change the frame's background color:

frame.getContentPane().setBackground(Color.BLACK);
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
1

Change the color of the JPanel you are adding, not the JFrame. You won't see the black JFrame if you add a white JPanel on top of it!

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ChangeColor extends JPanel {
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    }

    public static void createGUI() {
        JFrame frame = new JFrame();

        ChangeColor c = new ChangeColor();
        c.setBackground(Color.BLACK);

        frame.add(c);
        frame.setSize(1960, 1070);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        frame.setResizable(false);
    }
}
sleepToken
  • 1,866
  • 1
  • 14
  • 23
  • @PM77-1 definitely not what I would have done - I just left it the way OP had it setup. I'd recommend extending JFrame and creating a new JPanel instead. – sleepToken Dec 27 '19 at 15:23
  • @PM77-1 Genuinely curious here - what's the difference? In each the main class is extending JPanel and it is adding *itself* to a JFrame that belongs to an instance of itself, no? – sleepToken Dec 27 '19 at 15:31
  • 1
    My bad. I misread your code. Sorry about that. – PM 77-1 Dec 27 '19 at 15:33
  • @PM77-1 no worries - you definitely had me second guessing myself lol – sleepToken Dec 27 '19 at 15:36
  • @NoobyProgrammer because “more complicated” is the *correct* way to do it. Did you actually read my answer? Do some research and you will find this is required in all Swing GUIs for thread safety. The simplest “one line of code” changes are not always the best. – sleepToken Dec 29 '19 at 00:43
  • um... i am kind of a beginner to java and I really dont know what SwingUtilities is – Laser Infinite Dec 29 '19 at 19:23
  • @NoobyProgrammer check out www.google.com - it's a site that will let you research any topic you wish to learn about. – sleepToken Dec 29 '19 at 20:09