import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** * Demonstrates an if else chain * @author Mr J * @version 021203 */ public class WhereToday extends Applet implements ActionListener { // sample awt objects private TextField name = new TextField("Where do you want to go?"); private Button pressMe = new Button("Press me!"); private Label greeting = new Label("Enter m - movies; c - cafe or p - pub"); /** * Add objects to the Applet */ public void init() { add(name); add(pressMe); add(greeting); // Causes button presses to be detected pressMe.addActionListener(this); } /** * When an event occurs on an object with an ActionListener attached, this * method is carried out. * * @param e carries details about the event that occurred */ public void actionPerformed(ActionEvent e) { // get user input and output message: String choice = name.getText(); choice = choice.substring(0,1); String answer = ""; if ( (choice.equals("p")) || (choice.equals("P")) ) { answer = "But just a soft drink for me, thanks!"; answer = answer.substring(4, 17); } else if (choice.equals("m")) { answer = "What shall we watch?"; } else if (choice.equals("c")) { answer = "OK, I will have a cup of tea..."; } else { answer = "I don't know where that is"; } greeting.setText(answer); } }