//******************************************************************** // GUICalculator.java Author: Sophie Quigley // // Provides a graphical user interface for the Calculator Class // //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUICalculator { private static final boolean ONE = true; // One entry on stack will be cleared private static final boolean ALL = false; // Whole stack should be cleared private static final char PLUSLABEL = (char) 0x002B; private static final char MINUSLABEL = (char) 0x002D; private static final char TIMESLABEL = (char) 0x00D7; private static final char DIVLABEL = (char) 0x00F7; private Calculator calculator; private JFrame frame; private JPanel panel; private JLabel instructions1, instructions2, instructions3; private JTextField number; private JButton clearone, clearall; private JButton[] operation; private Character[] oplabel = {new Character(PLUSLABEL),new Character(MINUSLABEL), new Character(TIMESLABEL),new Character(DIVLABEL)}; public GUICalculator () { int i; calculator = new Calculator(); calculator.addViewer(Calculator.GRAPHICSVIEWER); frame = new JFrame("Reverse Polish Notation Calculator"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Create labels, buttons and text field instructions1 = new JLabel ("Enter a number and press enter"); instructions2 = new JLabel ("or press an operation button. "); instructions3 = new JLabel ("You can also correct your inputs:"); number = new JTextField (10); clearone = new JButton("Clear last entry"); clearall = new JButton("Clear all entries"); operation = new JButton[4]; for (i=0; i<4; i++) operation[i] = new JButton(oplabel[i].toString()); // Add labels buttons and text field to panel. panel = new JPanel(); panel.setPreferredSize (new Dimension(200, 200)); panel.setBackground (Color.cyan); panel.add (instructions1); panel.add (number); panel.add (instructions2); for (i=0; i<4; i++) panel.add (operation[i]); panel.add (instructions3); panel.add (clearone); panel.add (clearall); frame.getContentPane().add (panel); // Add event drivers for buttons and text field number.addActionListener(new NumberListener()); clearone.addActionListener(new ClearListener(ONE)); clearall.addActionListener(new ClearListener(ALL)); for (i=0; i<4; i++) operation[i].addActionListener(new OperationListener(i)); } //----------------------------------------------------------------- // Displays the primary application frame. //----------------------------------------------------------------- public void display() { frame.pack(); frame.show(); } //***************************************************************** // Action listener for the number input field. //***************************************************************** private class NumberListener implements ActionListener { // Performs action when enter key is pressed in the text field public void actionPerformed (ActionEvent event) { calculator.number(new Float(number.getText())); number.setText(""); } } // Action listener for the buttons private class ClearListener implements ActionListener { private boolean one; public ClearListener (boolean one) { this.one = one; } public void actionPerformed (ActionEvent event) { if (one) calculator.eraseLast(); else calculator.clear(); } } // Action listener for the operation buttons private class OperationListener implements ActionListener { private int opcode; public OperationListener(int opcode) { this.opcode = opcode; } public void actionPerformed (ActionEvent event) { number.setText(""); calculator.operation(opcode); } } }