WHAT'S NEW?

Swing Program to Change Color on Click of Button

Program discussed below have 4 JButton's arranged in following manner.


When the buttons Red,Green and Blue are clicked it changes the color of Display button.



DisplayColor.java


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class DisplayColor extends JFrame implements ActionListener 
{
   JButton jbtnDisplay,jbtnRed,jbtnGreen,jbtnBlue;
   Color cR=Color.RED;
   Color cG=Color.GREEN;
   Color cB=Color.BLUE;

   DisplayColor()
{
       super ("DISPLAY COLOR");
  setSize(1000,500);
  setResizable(false);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
       setLayout(null);
  Container c=getContentPane();

  jbtnDisplay=new JButton("");
  jbtnDisplay.setBounds(200,100,500,200);
  jbtnDisplay.setBackground(Color.BLACK);
  jbtnDisplay.addActionListener (this);
  c.add (jbtnDisplay);


       jbtnRed=new JButton("RED");
  jbtnRed.setBounds(50+100,350,100,50);
  jbtnRed.addActionListener (this);
  c.add (jbtnRed);
 
  jbtnGreen=new JButton("GREEN");
  jbtnGreen.setBounds(250+100,350,100,50);
  jbtnGreen.addActionListener (this);
  c.add (jbtnGreen);
   
        jbtnBlue=new JButton("BLUE");
   jbtnBlue.setBounds(450+100,350,100,50);
  jbtnBlue.addActionListener (this);
  c.add (jbtnBlue);
          
 setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
        if (ae.getSource()==(jbtnRed))
        {
jbtnDisplay.setBackground(Color.RED);
        }
if (ae.getSource()==(jbtnGreen))
        {
jbtnDisplay.setBackground(Color.GREEN);
        }
if (ae.getSource()==(jbtnBlue))
        {
jbtnDisplay.setBackground(Color.BLUE);
        }

}

  public static void main (String [] args)
{
  DisplayColor app=new DisplayColor();
  }
}

1 comment: Leave Your Comments

  1. Live casinos take your common recreation of roulette and make it much more 카지노사이트 immersive. After all, a real vendor will spin a real wheel with a ball, and you’ll be able to|be capable of|have the flexibility to} hear the identical acquainted sound of the wheel as you'd at a land-based on line casino. Asia is the place the place Macau, Singapore, and the Philippines are situated and every passionate player is dreaming of rolling the dice in one of the exclusive on line casino resorts there.

    ReplyDelete