/**
* Title: Using Multiple Buttons<p>
* Description: Push a button to answer a question<p>
* Copyright: Copyright (c) Howard Tse<p>
* Company: Bayview Secondary<p>
* @author Howard Tse
* @version 1.0
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class multiple_buttons2 extends JApplet implements ActionListener
{
JLabel[] pictures = new JLabel [3];
JButton[] buttons = new JButton [3];
JLabel text = new JLabel ("Press a button for the answer: ");
public void init ()
{
Icon picture1 = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/flag.gif");
Icon picture2 = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/monkey.gif");
Icon picture3 = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/book.jpg");
pictures [0].setIcon (picture1);
pictures [1].setIcon (picture2);
pictures [2].setIcon (picture3);
getContentPane ().setLayout (new GridLayout (3, 2));
getContentPane ().add (text);
String[] buttonLabels = {"Button 1: What is the Capital of Zaire?", " Button 2: What was the name of the first monkey in space?", "Button 3: What is one of ms ketter's favorite books?"};
for (int i = 0 ; i < 3 ; i++)
{
buttons [i] = new JButton (buttonLabels [i]);
buttons [i].addActionListener (this);
getContentPane ().add (buttons [i]);
getContentPane ().add (pictures [i]);
}
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == buttons [0])
{
text.setText ("Capital: Kinshasa.");
}
else if (e.getSource () == buttons [1])
{
text.setText ("First monkey in space: Gordo");
}
else if (e.getSource () == buttons [2])
{
text.setText ("Ms Ketter's Favorite book: James and the Giant Peach");
}
}
}
|