import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** * Adds two numbers together! * Lines changed from PressMe * are indicated with this symbol: // <<<<<<<<<<<<<<< * * @author Mr J * @version 281003 */ public class AddTwo extends Applet implements ActionListener { private TextField number1 = new TextField(20); // <<<<<<<<<<<<<<< private TextField number2 = new TextField(20); // <<<<<<<<<<<<<<< private Button pressMe = new Button("Add"); // <<<<<<<<<<<<<<< private Label greeting = new Label("The answer"); // <<<<<<<<<<<<<<< /** * Add objects to the Applet */ public void init() { add(number1); // <<<<<<<<<<<<<<< add(number2); // <<<<<<<<<<<<<<< add(greeting); add(pressMe); pressMe.addActionListener(this); } /** * When the button is pressed, do the calculation * * @param e carries details about the event that occurred */ public void actionPerformed(ActionEvent e) { // assume user types in whole numbers only int x, y; // int is for whole numbers // <<<<<<<<<<<<<<< x = Integer.parseInt(number1.getText()); // <<<<<<<<<<<<<<< y = Integer.parseInt(number2.getText()); // <<<<<<<<<<<<<<< greeting.setText("Answer: " + (x + y)); // <<<<<<<<<<<<<<< } }