Java Program to Change the Background Color of the Frame on Clicking the Respective Color Buttons

Overview

In this blog, we will learn how to change a frame’s background color when its respective color buttons are clicked using the concept of Swing in Java programming language.

Program Description

The aim of this program is to change the background color of the frame whenever the user clicks the respective color buttons. For example, consider the frame given below; if the button “blue” is clicked, the background color of the frame should change from black to blue.

How Will the Output Look Like?

Output frame

What is Swing?

Swing provides a set of classes that provides more powerful and flexible GUI components than does the AWT. Since they are light weight components, they provide a dynamic look and feel.

A swing GUI consists of two key items: components and containers.

A component is an independent visual control, such as a push button or slider.

All of Swing’s components are represented by classes defined within the package “javax.swing”.

Some of the swing components are: JButton, JLabel, JList, JRadioButton, etc.

In general, Swing components are derived from the “JComponent” class.

A container holds a group of components.

The top-level swing containers are: JFrame, JApplet, JWindow and JDialog.

These containers do not inherit “JComponent” class.

Source Code

Here is the complete source code for the Java program that changes the background color of the frame on clicking the respective color buttons. Check out the code explanation which is given below for clear understanding of the program.

Program Explanation

JFrame frame = new JFrame("Colourful window");
JButton red = new JButton("RED");

JFrame class is used to create a new JFrame container with the given text as the frame’s title.

JButton class is used to create button with the given text as content of the button.

Color c = new Color(255, 255, 255);
Font font = new Font("Lucida Calligraphy", Font.BOLD, 9);
red.setBounds(40, 100, 100, 40);
red.setBackground(c);
red.setFont(font);

Color class with RGB values as parameters, is used to set color to the component.

Font class is used to set font for the GUI component. It has font name, font style and font size as its parameters.

The setBounds() method is used for the button to specify the position and size of the button. Its syntax is setBounds(x, y, width, height).

The setBackground() method is used to set background color to the button.

Also, the setFont() method is used to set font to the button.

frame.add(red);
red.addActionListener(this);

The add() method is used to add the button to the frame.

The ActionListener interface(with actionPerformed() as an interface method) is called when the button is clicked.

frame.getContentPane().setBackground(Color.black);
frame.setLayout(null);
frame.setSize(650, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

The getContentPane() method retrieves the content pane layer so that you can add an object to it.

The setLayout() method is used to set a layout to the frame.

The setSize() method sets the width and height of the frame.

The setVisible() makes the frame appear on the screen.

Calling setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE), the frame will be closed.Program Explanation

public void actionPerformed(ActionEvent ae) 
{
if(ae.getSource() == red)
frame.getContentPane().setBackground(Color.RED);
}

The actionPerfomed() method is invoked automatically when you click on a button.

The getSource() method is used to determine which button was clicked. If the clicked button is red, then the frame’s background color is changed to red color.

Similarly, the other buttons are designed and functioned in the mentioned manner.

Program Output

The output frame is displayed for the program when the pink button is clicked.

When pink button is clicked

Are you looking to master coding and crack top company interviews? Go ahead and try GUVI’s Pro Subscription to help you with everything needed to build a strong Technical Career.

Know more about GUVI:

Also, download GUVI’s app to learn the latest IT skills in vernacular languages.

Love podcasts or audiobooks? Learn on the go with our new app.

Bookmarklets — The Productivity Hacker

Working with the HTTP Protocol in Julia

Will Bit Replace npm?

How to backup your object storage

Protect from CSRF attack in Node.js

Adobe After Effects animations in PubCoder with Bodymovin

Nullish Coalescing in TypeScript?? A Short Guide

Full-Stack Note App CI/CD Deployment on AWS Amplify Part-1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Baruni Priya T S

Baruni Priya T S

More from Medium

How to ease the maintenance of a low technical debt : Strategic Choices

Circle CI while waiting for ADO

How Mature Architecture Happens

CQS — About commands and queries

yin yang — or commands and queries