import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ButtonTest2 extends JFrame implements ActionListener {
    // a panel to hold all the buttons
    private JPanel buttonPanel = new JPanel(new GridLayout(1,0));
    // a label to show pictures
    private JLabel pictureLabel = new JLabel();
    // a list of buttons, with the animal names on it
    private JButton[] buttons =  { new JButton("cat"), 
	                           new JButton("dog"),
				   new JButton("bird"),
                                  new JButton("horse")};
    // animal				  
    private String animal = "cat";				  
    private Container contentPane = getContentPane();
    public ButtonTest2() {
	// create the buttons and addActionListener to all the buttons
	for(int i = 0; i < buttons.length; i++) {
	    buttonPanel.add(buttons[i]);
	    buttons[i].addActionListener(this);
	}
	contentPane.add(buttonPanel, BorderLayout.NORTH);
	contentPane.add(pictureLabel);
	pictureLabel.setHorizontalAlignment(JLabel.CENTER);
	pictureLabel.setOpaque(true);
	pictureLabel.setBackground(Color.yellow);
	setLabelPicture(); // see below
    }
    
    // set the icon of the pictureLabel
    private void setLabelPicture() {
	String filename = animal + ".jpg";
	pictureLabel.setIcon(new ImageIcon(filename));
    }
    
    public void actionPerformed(ActionEvent e) {
	// return the text from the source.
	animal = e.getActionCommand(); 
	setLabelPicture();
    }
    
    public static void main(String[] args) {
        JFrame f = new ButtonTest2();
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.setSize(800,600);
	f.setVisible(true);
    }
}

				   
