1

I simplified my original code to this problematic part. I know it seems quite simple, that's why it confused me when it doesn't work as I intended. It has no compilation or runtime errors yet I have failed to set the background color anything other than default. I've tried to create a new Color instance and invert the setVisible and setBackground methods, which wouldn't make a difference and my Google search wouldn't show anything different than I have done.

import javax.swing.JFrame;
import java.awt.Color;

public class Main extends MyFrame{
    public static void main(String[]args){
        //new MyFrame();
        JFrame frame=new JFrame("Title");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.red);
        frame.setVisible(true);
    }
}
Emin K.L.
  • 23
  • 3

1 Answers1

0

Code:

import javax.swing.JFrame;
import java.awt.Color;

public class Main extends MyFrame {
    
    public static void main(String[]args){
        JFrame frame = new JFrame("Title");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.RED); // <-----
        frame.setVisible(true);
    }
}

Result:

enter image description here

Agzam
  • 138
  • 1
  • 6