How to Change JFrame Groundwork Color in Java

Howdy, enthusiasts! In this tutorial, we volition hash out how to alter the JFrame background color in Java.

Changing JFrame Background Color in Java

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

  • How to change JLabel font style and size in Java

  • How to Exit JFrame on Close in Java Swing

We apply the post-obit lawmaking for the creation of a JFrame:

JFrame f = new JFrame(); // Cosmos of a JFrame object named equally f        f.setTitle("Change Background Color"); //To set the title of the frame f.setVisible(truthful); // To present the frame on the screen f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //To close the JFrame and the code on close push 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 evidently white groundwork. Now, nosotros have to change the background color as per our requirements. We utilize the setBackground() component of the JFrame course to set the background colour.

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. Permit united states discuss each one of them in particular.

Two Approaches to Change the Background Colour of JFrame

Method 1: Using Color Fields

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

Permit the states look at the code:

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

Output:

Changing JFrame Background Color in Java

Method ii: Using Colour Constructors

By using the constructors of the Color Class, nosotros can define the RGB values of a color. This makes the states enable all the colors by simply specifying the properties of the color. We can as well specify the opacity as the quaternary parameter. In the code, we created a color object which specifies the RGB values of the color and then laissez passer this as a parameter to the setBackground() role.

Permit us expect at the code:

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

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 contained 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 Colour"); //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 lawmaking on close push 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 lot found this post useful!