Background color change on Button Click in JAVA

background

Hi again my amazing programmers. Today we will learn some Java programming. In this tutorial we will see how to change the frame background color. All is based on user button click.

To change the background color by using multiple buttons and multiple conditions we do the following steps :

We create the class buttons_clicked in which are given 4 buttons and a label.

  1. JButton btn -> changes the label text
  2. JButton red,blue,green -> change the label text and the background color
private JButton btn;
private JButton red,blue,green;
private JLabel label;

After declaring these variables , we have to initialize them and add them to frame. Of course we should extend the JFrame class. And also implement the ActionListener interface.(to interact with user clicks)

We initialize everything in the constructor so we can call it in out main method.

The constructor :

public buttons_clicked()
  {
    btn = new JButton("Click here to change label");
    btn.addActionListener(this);
    add(btn);
    
    red = new JButton("red");
    red.addActionListener(this);
    add(red);
    
    green = new JButton("green");
    green.addActionListener(this);
    add(green);

    blue = new JButton("blue");
    blue.addActionListener(this);
    add(blue);
    
    setLayout(new FlowLayout());
    setSize(700,700);
    setTitle("Bit Life - Java program Buttons Clicked");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    
    label = new JLabel("what is happening ?");
    add(label);
    
  }

 

The logic :

public void actionPerformed(ActionEvent e) 
  {
    if(e.getSource() == btn)
    {
      label.setText("button clicked");
      
    }
    
    if (e.getSource() == red)
    {
      label.setText("red selected");
      getContentPane().setBackground(Color.RED);
      
    }
    
    if (e.getSource() == green)
    {
      label.setText("green selected");
      getContentPane().setBackground(Color.GREEN);
    }
    
    if(e.getSource() == blue)
    {
      label.setText("blue selected");
      getContentPane().setBackground(Color.BLUE);
    }
  }

In order to interact with the user we need to implement the actionPerformed() method of the ActionListener interface. It takes a single parameter -> (ActionEvent e) which is just the user click.

  1. if(e.getSource() == btn) -> means that if the user clicks the ‘btn’ Button then our label will change the text to “button clicked”
  2. if (e.getSource() == red) -> means that if the user clicks the ‘red’ Button the frame background with change to RED color. It will also change the text in our label to “red selected”
  3. if (e.getSource() == green) -> means that if the user clicks the ‘green’ Button the frame background with change to GREEN color. It will also change the text in our label to “green selected”
  4. if (e.getSource() == blue) -> means that if the user clicks the ‘blue’ Button the frame background with change to BLUE color. It will also change the text in our label to “blue selected”

The full and compiled program :

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class buttons_clicked extends JFrame implements ActionListener{
  
  private JButton btn;
  private JButton red,blue,green;
  private JLabel label;
  
  public buttons_clicked()
  {
    btn = new JButton("Click here to change label");
    btn.addActionListener(this);
    add(btn);
    
    red = new JButton("red");
    red.addActionListener(this);
    add(red);
    
    green = new JButton("green");
    green.addActionListener(this);
    add(green);

    blue = new JButton("blue");
    blue.addActionListener(this);
    add(blue);
    
    setLayout(new FlowLayout());
    setSize(700,700);
    setTitle("Bit Life - Java program Buttons Clicked");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    
    label = new JLabel("what is happening ?");
    add(label);
    
  }
  

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

  
  public void actionPerformed(ActionEvent e) 
  {
    if(e.getSource() == btn)
    {
      label.setText("button clicked");
      
    }
    
    if (e.getSource() == red)
    {
      label.setText("red selected");
      getContentPane().setBackground(Color.RED);
      
    }
    
    if (e.getSource() == green)
    {
      label.setText("green selected");
      getContentPane().setBackground(Color.GREEN);
    }
    
    if(e.getSource() == blue)
    {
      label.setText("blue selected");
      getContentPane().setBackground(Color.BLUE);
    }
  }

}

 

background color

Hope you enjoyed this example. If you have any questions feel free to comment.

LEAVE A REPLY

Please enter your comment!
Please enter your name here