import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/** Never write the same lines code twice use loops. */

public class SheepDollyLoop extends Applet
{
    // Variable beloging to our class:
    TextField info;
    Button dolly;

    // We're going to bind this function to each sheep Dolly?.
    void dollyClicked(String name)
    {
	// Change label string.
	info.setText(name + " clicked.");
    }    

    // init() is called when the applet appears on the screen.
    public void init()
    {
	// Use GridLayout to enable us to place our components in a line.
	setLayout(new GridLayout(1,6));
	
	// Our newspaper.
	info = new TextField("No dolly clicked yet.");
	add(info);
	
	// Fife identical sheep dolly will be born soon.
	for(int n=1; n <= 5; n++) { 

	    // Dolly nummer n will be born.
	    dolly = new Button();

	    // Baptism of Dolly number n.
	    dolly.setLabel("I am Dolly" + n);

	    // Public presentation of Dolly number n.
	    add(dolly);

	    // Now we change habit. Is that ethical?
	    dolly.addActionListener(new DollyListener("Dolly" + n));
	}
    }

    /** An action listener which remembers Dollys name. */
    class DollyListener implements ActionListener {

	// Name of Dolly.
	final String name;

	DollyListener(String n) {
	    // remember Dollys name.
	    name = n;
	}

	public void actionPerformed(ActionEvent e)
	{
	    // Since we are yet in the class SheepDollyLoop we can
	    // call the method dollyClicked(String).
	    dollyClicked(name);
	}
    } // end DollyListener

} // end SheepDollyLoop

