Write a java program to create Button Click event to set the background color.
import java.awt.*;
import java.awt.event.*; class Sample1 extends Frame implements ActionListener { Sample1() { setLayout(null); Button b1,b2,b3; b1= new Button("yellow"); b2= new Button("red"); b3= new Button("green"); b1.setBounds(50,100,50,30); b2.setBounds(50,140,50,30); b3.setBounds(50,180,50,30); add(b1); add(b2); add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); addWindowListener (new WindowAdapter() { public void WindowColsing(WindowEvent we) { System.exit(0); } });} public void actionPerformed(ActionEvent ae) { String s; s = ae.getActionCommand(); if(s.equals("yellow")) setBackground(Color.yellow); if(s.equals("red")) setBackground(Color.red); if(s.equals("green")) setBackground(Color.green); } public static void main (String args[ ]) { Sample1 s1; s1 = new Sample1(); s1.setSize(400,500); s1.setVisible(true); } } |