The action method lets you handle the activation of a component, i.e. a user action directod at a widget.
Any time an event occurs in a Component.
import java.applet.Applet;
import java.awt.Label;
import java.awt.Button;
import java.awt.Event;
import java.util.Vector;
public class Fortune extends Applet {
Vector fortunes;
Label l;
public void init() {
l = new Label("Your Fortune for a nickel");
add(l);
add (new Button("A Nickel"));
int i = 1;
String nextline;
fortunes = new Vector();
while ((nextline = getParameter("fortune" + i++)) != null) {
fortunes.addElement(nextline);
}
}
public boolean action(Event e, Object o) {
if (e.target instanceof Button) changeFortune();
return true;
}
public void changeFortune() {
int i = (int) (Math.random() * fortunes.size());
l.setText((String) fortunes.elementAt(i));
}
}