//Counting.java //Demonstrate action-handling with multiple buttons //By Andy Harris, 4 / 00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class Counting 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"); Button btnThree = new Button("Three"); Button btnFour = new Button("Four"); 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); add(btnThree); add(btnFour); //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); btnThree.addActionListener(this); btnFour.addActionListener(this); } // end init public void actionPerformed(ActionEvent e){ //get the name of the button pressed String theCommand = e.getActionCommand(); //check to see which button was pressed if (theCommand.equals("One")){ lblOutput.setText("Ichi"); } else if (theCommand.equals("Two")){ lblOutput.setText("Nii"); } else if (theCommand.equals("Three")){ lblOutput.setText("San"); } else if (theCommand.equals("Four")){ lblOutput.setText("Shi"); } else { lblOutput.setText("Some error occurred"); } // end if } // end actionPerformed } // end class def