import java.awt.*; import java.awt.event.*; /** * A very simple calculator. This will read two integers from the user * and display their sum. * * Uses a simple graphical user interface, rather than textual * input and output. * * The interface will present two text areas in which the user can * enter numbers, an ``equals'' button that the user presses to * request a result, and an area in which the program can write * output. * * The class serves as the listener for its own frame and button, * providing actionPerformed and windowClosing methods. * * @author Samuel A. Rebelsky * @version 1.1 of February 1999 */ public class SimpleCalc extends WindowAdapter implements ActionListener { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The frame used for interaction. */ protected Frame gui; /** The field used for input of the first value. */ protected TextField val1; /** The field used for input of the second value. */ protected TextField val2; /** The area used to show the plus sign. */ protected Label plus; /** The area used for output. */ protected Label out; /** The area used for messages (errors and others). */ protected Label messages; /** The button used to tell it to compute. */ protected Button equals; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** Set up the frame for interaction. */ protected SimpleCalc() { // Create the frame. gui = new Frame("Add"); gui.setLayout(new FlowLayout()); gui.addWindowListener(this); // Create the output field. Leave sufficient space for output. out = new Label(" "); // Create the messages field. Begin with instructions. messages = new Label("Enter two integers and click the = button."); // Create the input fields. Note that we initialize them to have // some spaces to ensure that the layout manager gives us enough // space. A little later, we'll clear them out. val1 = new TextField(" "); val2 = new TextField(" "); // Create the addition symbol. plus = new Label("+"); // Create the button. equals = new Button("="); equals.addActionListener(this); // Add the various components gui.add(messages); gui.add(val1); gui.add(plus); gui.add(val2); gui.add(equals); gui.add(out); gui.pack(); // Clear the text fields (see above). val1.setText(""); val2.setText(""); // And we're ready to go. gui.show(); } // SimpleCalc() // +----------------+------------------------------------------ // | Helper Methods | // +----------------+ /** Do the simple computation. */ public void doComputation() { // The two values we'll use to compute. int num1 = 0; int num2 = 0; // Read the strings the user entered. String str1 = val1.getText(); String str2 = val2.getText(); // Convert the first value to a number. try { num1 = Integer.valueOf(str1).intValue(); } catch (Exception e) { // Whoops! Bad input. Tell the user. messages.setText("The left operand is not an integer!"); // Reset the input. val1.setText(""); // Give up. return; } // Convert the second value to a number. try { num2 = Integer.valueOf(str2).intValue(); } catch (Exception e) { // Whoops! Bad input. Tell the user. messages.setText("The right operand is not an integer!"); // Reset the input. val2.setText(""); // Give up. return; } // Display the result. out.setText(Integer.toString(num1+num2)); } // doComputation() // +----------------+------------------------------------------ // | Event Handlers | // +----------------+ /** React to a click on the button. */ public void actionPerformed(ActionEvent evt) { doComputation(); } // actionPerformed(ActionEvent) /** React to the closing of the window. */ public void windowClosing(WindowEvent event) { gui.dispose(); System.exit(0); } // windowClosing() // +------+---------------------------------------------------- // | Main | // +------+ /* Start the ball rolling by creating an object. */ public static void main(String[] args) { new SimpleCalc(); } // main(String[]) } // class SimpleCalc