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

25 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.

  3. Thank you for the sensible critique. Me & my neighbor were just preparing to do some research on this. We got a grab a book from our local library but I think I learned more clear from this post. I’m very glad to see such magnificent information being shared freely out there.

  4. If some one desires expert view on the topic of blogging and site-building afterward i suggest him/her to pay a quick visit
    this weblog, Keep up the fastidious job.

  5. Thanks oon youг marvelous posting! Ι գuite enjoyed reading it, you could bee a grеat author.
    I willl mɑke surе to bookmark уouг blog аnd will cоme baack at s᧐me ρoint.
    Iwant tօ encourage үou tto definittely continue үоur great
    job, have a nic morning!

  6. Generic for cialis

    Admiring the persistence you put into your website and in depth information you offer. It’s good to come across a blog every once in a while that isn’t the same unwanted rehashed material. Excellent read! I’ve saved your site and I’m adding your RS…

  7. I like the valuable info you pdovide in your articles.
    I’ll bookmark your weblog and check again here regularly.
    I’m quite certain I’ll learn a lott of new stff right here!
    Good luck for the next!

  8. Heya i’m for the first time һere. I cɑme aϲross this board аnd I to fіnd
    It гeally useful & it helped me out ɑ ⅼot. Ӏ’m hoping to pгesent sometһing agɑin and aid others like you aided me.

  9. We’re a bunch of volunteers and starting a new scheme in our community.
    Your website offered us with useful information to work on. You’ve done a formidable activity and our whole
    neighborhood can be grateful to you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here