/* File: Clicks.java */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Clicks1 extends Applet implements ActionListener{ private Button leftButton = new Button("Not Clicked On Yet"); private Button rightButton = new Button("Not Clicked On Yet"); private Button middleButton = new Button("Total number of clicks : 0"); private int clickCountLeft = 0, clickCountRight = 0, clickCount = 0; public void init() { //Add leftButton to the Applet leftButton.setBackground(Color.red); add(leftButton); //Register Clicks as a listener for leftButton leftButton.addActionListener(this); //Add middleButton to the Applet middleButton.setBackground(Color.lightGray); add(middleButton); //Add rightButton to the Applet rightButton.setBackground(Color.blue); add(rightButton); //Register Clicks as a listener for rightButton rightButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { //Determine which button was clicked on if (e.getSource() == leftButton) { //Change color after each press if (leftButton.getBackground() == Color.red) { leftButton.setBackground(Color.green); middleButton.setBackground(Color.green); } else { leftButton.setBackground(Color.red); middleButton.setBackground(Color.red); } clickCountLeft++; clickCount++; leftButton.setLabel("Click count: " + clickCountLeft); // declare that the layout is no longer valid leftButton.invalidate(); } else if (e.getSource() == rightButton) { //Change color after each press if (rightButton.getBackground() == Color.blue) { rightButton.setBackground(Color.cyan); middleButton.setBackground(Color.cyan); } else { rightButton.setBackground(Color.blue); middleButton.setBackground(Color.blue); } clickCountRight++; clickCount++; rightButton.setLabel("Click count: " + clickCountRight); // declare that the layout is no longer valid rightButton.invalidate(); } middleButton.setLabel("Total number of clicks : " + clickCount); middleButton.invalidate(); // have the applet re-do the layout. validate(); } } // class ClickMe