Learning to use Buttons
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class button extends Applet implements ActionListener
{
Button redButton = new Button("Red");
Button blueButton = new Button("Blue");
Button greenButton = new Button("Green");
Button whiteButton = new Button("White");
Button blackButton = new Button("Black");
public void init() {
setBackground(Color.red);
add(redButton);
add(blueButton);
add(greenButton);
add(whiteButton);
add(blackButton);
redButton.addActionListener(this);
blueButton.addActionListener(this);
greenButton.addActionListener(this);
whiteButton.addActionListener(this);
blackButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
setBackground(Color.white);
if (evt.getSource() == redButton)
setBackground(Color.red);
else if (evt.getSource() == blueButton)
setBackground(Color.blue);
else if (evt.getSource() == greenButton)
setBackground(Color.green);
else if (evt.getSource() == whiteButton)
setBackground(Color.white);
else setBackground(Color.black);
repaint();
}
}