How to change the Background color of the JFrame in Java

In this tutorial, we will learn and explore how can we change the Background color of the JFrame in Java.

JFrame is a type of container imported from javax.swing package which inherits the java.awt.Frame class. When a JFrame is created, it develops a window that works like the main window where all the components like JLabel, JButton, JTextBoxes are inserted or added to created a Java GUI Application. To make GUI Application look more attractive we can change the background color.

So this tutorial is all about JFrame background

Change the Background color of the JFrame in Java

Before changing the background color, we need to have a JFrame Window that can be created using Java swing libraries. To get started we need to import the Java Swing and Java Awt Packages.
So let’s get started with coding:

CODE:

jframe(){
        // Since I am coding inside a constructor 
        // that’s why no object is created or else this 
        // keyword can be used but that’s not necessary

        setTitle("JFrame Backgorund Color");
        setBounds(200,200,500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

 

OUTPUT:

How to change the Background color of the JFrame in Java

The output above shows a JFrame Window with a blank semi-white color background.  Now, we need to change the background color of this window. This work is done with the help of the setBackground() Component of the JFrame class.

Before using the setBackground() component first we need to have a container or content layer. For this getContentPane() component is used which retrieves the content layer so that later on if some other objects need to be added then we can simply add this in that contentPane.

Methods of changing the Background Color of Jframe

There are two ways by which we can change the background color :

 (1)  Color Constructor

 (2)  Color Class

Color Constructor: By using the color contractor class of the Java awt package. We can create many variations of color using RGB values. This not only allows us to can a limitless variety of colors but also helps in adjusting the opacity of the color. After adjusting the color and opacity we add the object name to the setBackground() component. To illustrate this code snippet is given below.

//Color Contructor
        Color mycolor=new Color(255,124,200);
        Container c =getContentPane(); // Container c having the getcontentpane
        c.setBackground(mycolor); //setting the Background Color

 

OUTPUT:

How to change the Background color of the JFrame in Java

Color class: We can also use the Color class to define the background color for the JFrame. There are predefined colors in the Java awt package. But there is only a disadvantage that the color defined is very limited and cannot be changed. To illustrate this code snippet is given below.

Container ck =getContentPane();
ck.setBackground(Color.red);

//Color class with color red

OUTPUT:

How to change the Background color of the JFrame in Java

 

To have a proper insight into the concept discussed, the whole code is illustrated below for further reference.

CODE:

import javax.swing.*; //Java Swing Package
// import java.awt.event.*;
import java.awt.*; // Java awt package

class JFrame extends JFrame{ //JFrame class extended by JFrame to inherit the function

    jframe(){
        // Since I am coding inside a contructor 
        // thats why no object is created or else this 
        // keyword can be used but thats not necessary

        setTitle("JFrame Backgorund Color");
        setBounds(200,200,500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       
        Color mycolor=new Color(255,124,200);
        Container c =getContentPane();// Container c having the getcontentpane
        c.setBackground(mycolor); //setting the Background Color

        
        Container ck =getContentPane();
        ck.setBackground(Color.red);//Color class with color red
        
        setVisible(true);

        

    }
    public static void main(String[] args) {
        new jframe();
    }

}

 

Hope this code provides a brief knowledge about JFrame and its background Changing properties.

Happy Coding !!!!!

2 responses to “How to change the Background color of the JFrame in Java”

  1. Abhijeet says:

    Very Good explanation Helped me to implement for the first time
    Thank you

  2. Nbab says:

    Thank you soo much. This help me so much

Leave a Reply

Your email address will not be published.