import java.awt.*; // // Dialog class // class ResultDialog extends Dialog { // Label for the total so far private Label totalSoFarLable = new Label (); // Continue button private Button continueButton = new Button ("Continue"); // Exit button private Button exitButton = new Button ("Exit"); // Constructor ResultDialog (Adder parentFrame) { // Call the parent constructor, passing the parent frame // reference, title, and true (for setting the modal true for // the dialog). super (parentFrame, "The total so far ....", true); // Set the layout setLayout (new FlowLayout ()); // Set the result label totalSoFarLable.setText ("The total so far is " + parentFrame.totalSoFar + " " + "Would you like to keep adding?"); // // Add the components // // Add the label add (totalSoFarLable); // Add the continue button add (continueButton); // Add the exit button add (exitButton); } // Handle the actions on the dialog public boolean action (Event event, Object what) { // If the action was on the add button if (event.target == continueButton) { // Remove the dialog window dispose (); } // If the action was on the exit button else if (event.target == exitButton) { // Close down System.exit (0); } // return true return true; } } // // Frame class // public class Adder extends Frame { // The welcome label private Label welcomeLabel = new Label ("Welcome to the Adder application!"); // The instructions private Label instructionsLabel = new Label ("Enter a number and I will keep track of the total for you"); // The add button private Button addButton = new Button ("Add"); // The text field for adding the numbers private TextField numberField = new TextField (20); // The total so far public int totalSoFar; // The label for the total so far private Label totalSoFarLable = new Label ("The total so far is " + totalSoFar); // Frame constructor public Adder () { // Call the super class, and set the window label super ("Adder Demo"); // Set the layout manager setLayout (new FlowLayout ()); // Add the welcome label add (welcomeLabel); // Add the instruction label add (instructionsLabel); // Add the number field add (numberField); // Add the add button add (addButton); // Add the total so far add (totalSoFarLable); } // Handle the actions on the Frame public boolean action (Event event, Object what) { // If the action was on the add button if (event.target == addButton) { try { // Get the number entered by the user int currentNumber = Integer.parseInt (numberField.getText ()); // Add to current total totalSoFar += currentNumber; // Create the dialog ResultDialog resultDialog = new ResultDialog (this); // Size it properly resultDialog.resize (300, 100); // Show the dialog box resultDialog.show (); // Reset the number field numberField.setText (""); // Reset the total label totalSoFarLable.setText ("The total so far is " + totalSoFar); } catch (Exception exception) { // Indicate error totalSoFarLable.setText ("Incorrect number entered! Try again!!"); } } // return true return true; } public static void main (String[] args) { // Create the adder Adder adder = new Adder (); // Resize adder.resize (300, 200); // Show the frame adder.show (); } }