//Counting2.java //Demonstrate action-handling with two buttons //By Andy Harris, 4 / 00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class Counting2 extends Applet implements ActionListener{ //Create some objects Label lblOutput = new Label("Learn to count in Japanese!"); Button btnOne = new Button("One"); Button btnTwo = new Button("Two"); public void init(){ //set up a Grid Layout with one column, as many rows as needed setLayout(new GridLayout(0,1)); //add the objects add(lblOutput); add(btnOne); add(btnTwo); //set up an 'applet-wide' default font this.setFont(new Font("SansSerif", Font.BOLD, 20)); //clean up the label lblOutput.setAlignment(Label.CENTER); lblOutput.setBackground(Color.yellow); //register the actionListener btnOne.addActionListener(this); btnTwo.addActionListener(this); } // end init public void actionPerformed(ActionEvent e){ //check to see which button was pressed String theCommand = e.getActionCommand(); if (theCommand.equals("One")){ lblOutput.setText("Ichi"); } else { lblOutput.setText("Nii"); } // end if } // end actionPerformed } // end class def