import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonClass extends Applet implements ActionListener {
Button red,white,blue;
Label hit;
public void init()
{
red=new Button("Red");
white=new Button("White");
blue=new Button("Blue");
hit=new Label("Hit a Button to Change the Screen Color");
add(red);
add(blue);
add(white);
add(hit);
red.addActionListener(this);
white.addActionListener(this);
blue.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Red"))
{
setBackground(Color.red);
}
else if(str.equals("White"))
{
setBackground(Color.white);
}
else
{
setBackground(Color.blue);
}
repaint();
}
}
Output :
import java.awt.*;
import java.awt.event.*;
public class ButtonClass extends Applet implements ActionListener {
Button red,white,blue;
Label hit;
public void init()
{
red=new Button("Red");
white=new Button("White");
blue=new Button("Blue");
hit=new Label("Hit a Button to Change the Screen Color");
add(red);
add(blue);
add(white);
add(hit);
red.addActionListener(this);
white.addActionListener(this);
blue.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Red"))
{
setBackground(Color.red);
}
else if(str.equals("White"))
{
setBackground(Color.white);
}
else
{
setBackground(Color.blue);
}
repaint();
}
}
Output :
No comments:
Post a Comment