change color

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.

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 which is just the user click.

  1. if(e.getSource() == btn) -> means that if the user click the btn Button then our label will change the text to “button clicked”
  2. if (e.getSource() == red) -> means that if the user click 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 click the red 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 click the red 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);
    }
  }

}

 

java change color

5 COMMENTS

  1. Having read this I thought it was really informative.
    I appreciate you spending some time and effort to put this information together.
    I once again find myself personally spending way too much time both reading and commenting.

    But so what, it was still worthwhile!

  2. Greetings I am so delighted I found your site, I really found you by mistake,
    while I was browsing on Digg for something else, Nonetheless I am here now and would just
    like to say thank you for a incredible post and a all round entertaining blog (I
    also love the theme/design), I don’t have time to look over
    it all at the moment but I have saved it and also included your RSS feeds,
    so when I have time I will be back to read a lot more, Please do keep up the awesome work.

LEAVE A REPLY

Please enter your comment!
Please enter your name here