// // "hint" program for homework #4 -- provide GUI for a very simple // "adding machine". // // see class definitions for additional info. // import java.awt.* ; import java.awt.event.* ; // // an AddMachine object represents a very primitive adding machine // (supporting only "add" and "clear" functions). // class AddMachine { private int balance = 0 ; int getBalance() { return balance ; } void clear() { balance = 0 ; } void add(int i) { balance += i ; } } // // an AddMachineGUI object provides a GUI for an AddMachine object, // consisting of a Label to display the current balance (also // used to display error messages), a TextField for entering // data, and the following Buttons: // "Clear" clears the AddMachine's balance to 0. // "Show" redisplays the balance (useful after an error message). // "Add" adds to the balance the number represented by the contents // of the TextField (if not numeric, an error message replaces // is shown in the balance-display area). // the object also disposes of itself and ends the program when // its window is closed. // class AddMachineGUI extends Frame implements ActionListener { AddMachine adder ; Label display ; TextField tf ; AddMachineGUI(AddMachine adder) { super("AddMachineGUI") ; this.adder = adder ; setLayout(new GridLayout(0, 1)) ; // add a window listener to respond to window-closing // events. addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose() ; System.exit(0) ; } } ) ; // display area display = new Label("", Label.CENTER) ; add(display) ; showBalance() ; // text field for entering number, in a Panel with // FlowLayout so it keeps its original size if // the window is resized. tf = new TextField(20) ; Panel p1 = new Panel(new FlowLayout()) ; p1.add(tf) ; add(p1) ; // buttons, grouped as a Panel with FlowLayout so // they keep their size. Panel p2 = new Panel(new FlowLayout()) ; p2.add(makeButton("Clear", "clear", this)) ; p2.add(makeButton("Show balance", "show", this)) ; p2.add(makeButton("Add", "add", this)) ; add(p2) ; pack() ; show() ; } // return a button with the specified label and action command // an an ActionListener of "this". Button makeButton(String label, String command, ActionListener l) { Button b = new Button(label) ; b.setActionCommand(command) ; b.addActionListener(l) ; return b ; } // handle ActionEvent (button press). public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand() ; if (cmd.equals("clear")) doClear() ; else if (cmd.equals("show")) doShow() ; else if (cmd.equals("add")) doAdd() ; else System.out.println("Something odd happened!") ; } void doClear() { adder.clear() ; showBalance() ; } void doShow() { showBalance() ; } void doAdd() { try { int num = Integer.parseInt(tf.getText()) ; adder.add(num) ; showBalance() ; } catch (NumberFormatException e) { showError("Input text is not a number") ; } } void showBalance() { display.setText("Balance = " + adder.getBalance()) ; } void showError(String s) { display.setText(s) ; } // // ---- main method -------------------------------------------- // // no command-line arguments. // public static void main(String[] args) { AddMachine adder = new AddMachine() ; AddMachineGUI gui = new AddMachineGUI(adder) ; } }