import java.applet.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class HelloApplet extends Applet implements ActionListener { private static final int ANSWER_LENGTH = 50; private TextField ageBox = new TextField (3); private Label ageLabel = new Label ("How old are you?"); private TextField answer = new TextField (ANSWER_LENGTH); private Button doItButton = new Button ("Go ahead!"); public void init () { this.add (this.ageLabel); this.add (this.ageBox); this.add (this.doItButton); this.add (this.answer); this.doItButton.addActionListener (this); } public void actionPerformed (ActionEvent whatHappened) { try { int age = Integer.parseInt (this.ageBox.getText()); if (age >= 18) { this.answer.setText ("" + age + " is old enough to vote. Congratulations!"); } else { this.answer.setText ("" + age + " is NOT old enough to vote."); } } catch (java.lang.NumberFormatException whatsWrong) { this.answer.setText ("I don't understand \"" + this.ageBox.getText() + "\"; the age must be an integer."); } } }