Source of CalculatorGUIAppSwing.java


  1: //CalculatorGUIApp.java
  2: //Emulates a simple calculator.

  4: import java.awt.*;
  5: import java.awt.event.*;
  6: import javax.swing.*;                                             //<----

  8: class Calculator
  9: extends JFrame                                                    //<----
 10: implements ActionListener
 11: {
 12:     private JTextField display = new JTextField(10);              //<----
 13:     private JPanel keypad = new JPanel();                         //<----
 14:     private JButton[] button = new JButton[15];                   //<----
 15:     private StringBuffer registerA = new StringBuffer();
 16:     private StringBuffer registerB = new StringBuffer();
 17:     private char operator;
 18:     private boolean firstNumberAlreadyInput = false;

 20:     static String[] keyLabels = {" 0 "," 1 "," 2 ",
 21:                                  " 3 "," 4 "," 5 ",
 22:                                  " 6 "," 7 "," 8 ",
 23:                                  " 9 "," + "," - ",
 24:                                  " = "," * "," / "};
 25:     //Constructor
 26:     public Calculator(String s)
 27:     {
 28:         super(s);
 29:         getContentPane().setBackground(Color.yellow);             //<----

 31:         //Set up display
 32:         getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER)); //<-
 33:         display.setEditable(false);
 34:         getContentPane().add(display);                            //<----

 36:         //Set up keypad
 37:         keypad.setLayout(new GridLayout(5,3));
 38:         for (int index=0; index!=15; index++)
 39:         {
 40:             button[index] = new JButton(keyLabels[index]);        //<----
 41:             button[index].setBackground(Color.white);
 42:             button[index].addActionListener(this);
 43:             keypad.add(button[index]);
 44:         }
 45:         getContentPane().add(keypad);

 47:         addWindowListener
 48:         (
 49:             new WindowAdapter()
 50:             {
 51:                 public void windowClosing(WindowEvent e)
 52:                 {
 53:                     System.exit(0);
 54:                 }
 55:             }
 56:         );
 57:     }

 59: 
 60:     //Method to respond to whichever key has been pressed
 61:     public void actionPerformed(ActionEvent event)
 62:     {
 63:         final int positionOfEqualsKey = 12;
 64:         Object source = event.getActionCommand();

 66:         //Test for digit in the range 0-9
 67:         for (int digit = 0; digit != 10; digit++)
 68:         {
 69:             if (source.equals(keyLabels[digit]))
 70:             {
 71:                 if (firstNumberAlreadyInput)
 72:                 {
 73:                     registerB.append(String.valueOf(digit));
 74:                     display.setText(registerB.toString());
 75:                     return;
 76:                 }
 77:                 else
 78:                 {
 79:                     registerA.append(String.valueOf(digit));
 80:                     display.setText(registerA.toString());
 81:                     return;
 82:                 }
 83:             }
 84:         }

 86:         //Test for an operator
 87:         for (int positionOfOperator  = 10;
 88:                  positionOfOperator != 15;
 89:                  positionOfOperator++)
 90:         {
 91:             //Test for +, -, * or /
 92:             if (source.equals(keyLabels[positionOfOperator]) && 
 93:                (positionOfOperator != positionOfEqualsKey))
 94:             {
 95:                 operator = keyLabels[positionOfOperator].charAt(1);
 96:                 firstNumberAlreadyInput = true;
 97:                 return;
 98:             }
 99:         }

101:         //Test for =
102:         if (source.equals(" = "))
103:         {
104:             display.setText(doCalculation());
105:             registerA.setLength(0);
106:             registerB.setLength(0);        
107:             firstNumberAlreadyInput = false;
108:         }
109:     }

111: 
112:     private String doCalculation()
113:     {
114:         final char beep = '\u0007';
115:         try
116:         {
117:             int m = new Integer(registerA.toString()).intValue();
118:             int n = new Integer(registerB.toString()).intValue();
119:         
120:             switch (operator)
121:             {
122:                 case '+' : return String.valueOf(m+n);
123:                 case '-' : return String.valueOf(m-n);
124:                 case '*' : return String.valueOf(m*n);
125:                 case '/' : return String.valueOf(m/n);
126:                 default  : return "   E R R O R";
127:             }
128:         }
129:         catch(Exception e)
130:         {
131:             System.out.print(beep);
132:             return "   E R R O R";
133:         }
134:     }
135: }


138: class CalculatorGUIAppSwing
139: {
140:     public static void main(String[] args)
141:     {
142:         Calculator myCalculator = new Calculator("Calculator");
143:         myCalculator.setSize(180, 200);                           //<----
144:         myCalculator.setVisible(true);
145:     }
146: }