How to Change JFrame Background Color in Java

Hello, enthusiasts! In this tutorial, we will discuss how to change the JFrame background color in Java. 

Changing JFrame Background Color in Java

We import the required libraries such as Java Swing and Java AWT to get started. We need to first create a JFrame using the Java Swing library. To get more insights into using Java Swing, Java AWT, Creation of JFrame, its components, etc, do visit these below posts to get more information:

We use the following code for the creation of a JFrame:

JFrame f = new JFrame(); // Creation of a JFrame object named as f       
f.setTitle("Change Background Color"); //To set the title of the frame
f.setVisible(true); // To present the frame on the screen
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //To close the JFrame and the code on close button
f.setBounds(100, 200, 350, 300); // To specify the location and size of the frame.

Output:

Changing JFrame Background Color in Java

We can see that we have created a JFrame with a plain white background. Now, we have to change the background color as per our requirements. We use the setBackground() component of the JFrame class to set the background color.

We firstly use the getContentPane() component to retrieve the content layer and to add the objects to our frame. In his case, we add various background color objects to our JFrame. There are two ways of doing this. Let us discuss each one of them in detail.

Two Approaches to Change the Background Color of JFrame

Method 1: Using Color Fields

The Colors are defined in the Java AWT package. By using the fields of the Color Class, we can set various background colors of our JFrame. The limitation of using these fields is that they contain only limited colors.

Let us look at the code:

//METHOD-1 : Using Color Fields
f.getContentPane().setBackground(Color.CYAN);

Output:

Changing JFrame Background Color in Java

Method 2: Using Color Constructors 

By using the constructors of the Color Class, we can define the RGB values of a color. This makes us enable all the colors by just specifying the properties of the color. We can also specify the opacity as the fourth parameter. In the code, we created a color object which specifies the RGB values of the color and then pass this as a parameter to the setBackground() function.

Let us look at the code:

//METHOD-2 : Using Color Constructors
Color color=new Color(255, 239 ,213);
f.getContentPane().setBackground(color);

Output:

How to Change JFrame Background Color in Java

For a greater understanding of all the concepts learned and to run the code on any independent machine, I have provided the entire code.

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

public class ChangeBackground extends JFrame {
    public static void main(String[] args) {

        JFrame f = new JFrame(); // Creation of a JFrame object named as f       
        f.setTitle("Change Background Color"); //To set the title of the frame
        f.setVisible(true); // To present the frame on the screen
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //To close the JFrame and the code on close button
        f.setBounds(100, 200, 350, 300); // To specify the location and size of the frame.

        //METHOD-1 : Using Color Fields
        f.getContentPane().setBackground(Color.CYAN);
        
        //METHOD-2 : Using Color Constructors
        Color color=new Color(255, 239 ,213);
        f.getContentPane().setBackground(color);
    }
}

I hope you found this post useful!

Leave a Reply

Your email address will not be published.