/* An implementation of a Graphical User Interface for the Calculator class. You should not modify this program as a part of your Programming Coursework. It is provided solely as a demonstration of object-oriented programming technique. TMK 8/2/01 Modified for JDK 2 implementation TMK 5/3/01 */ import java.awt.*; import javax.swing.*; public class CalculatorGUI extends javax.swing.JFrame { // This string will hold the contents of the screen at all times. String screen_text = ""; public CalculatorGUI() { Font f = new Font("Dialog", Font.BOLD, 18); // This code is automatically generated by Visual Cafe when you add // components to the visual environment. It instantiates and initializes // the components. To modify the code, only use code syntax that matches // what Visual Cafe can generate, or Visual Cafe may be unable to back // parse your Java file into its visual environment. //{{INIT_CONTROLS setTitle("CalculatorGUI"); getContentPane().setLayout(null); setSize(300, 420); setVisible(false); JButtonGroupPanel1.setLayout(new GridLayout(4, 4, 3, 3)); getContentPane().add(JButtonGroupPanel1); JButtonGroupPanel1.setBounds(24, 84, 254, 253); // Set the button text // Set the Action Command // Add the button to the group // Apply the correct font to each button // NB ORDER IS IMPORTANT FOR CORRECT LAYOUT b7.setText("7"); b7.setActionCommand("7"); JButtonGroupPanel1.add(b7); b7.setFont(f); b8.setText("8"); b8.setActionCommand("8"); JButtonGroupPanel1.add(b8); b8.setFont(f); b9.setText("9"); b9.setActionCommand("9"); JButtonGroupPanel1.add(b9); b9.setFont(f); badd.setText("+"); badd.setActionCommand("+"); JButtonGroupPanel1.add(badd); badd.setFont(f); b4.setText("4"); b4.setActionCommand("4"); JButtonGroupPanel1.add(b4); b4.setFont(f); b5.setText("5"); b5.setActionCommand("5"); JButtonGroupPanel1.add(b5); b5.setFont(f); b6.setText("6"); b6.setActionCommand("6"); JButtonGroupPanel1.add(b6); b6.setFont(f); bminus.setText("-"); bminus.setActionCommand("-"); JButtonGroupPanel1.add(bminus); bminus.setFont(f); b1.setText("1"); b1.setActionCommand("1"); JButtonGroupPanel1.add(b1); b1.setFont(f); b2.setText("2"); b2.setActionCommand("2"); JButtonGroupPanel1.add(b2); b2.setFont(f); b3.setText("3"); b3.setActionCommand("3"); JButtonGroupPanel1.add(b3); b3.setFont(f); bmultiply.setText("*"); bmultiply.setActionCommand("*"); JButtonGroupPanel1.add(bmultiply); bmultiply.setFont(f); bclear.setText("C"); bclear.setActionCommand("."); JButtonGroupPanel1.add(bclear); bclear.setFont(f); b0.setText("0"); b0.setActionCommand("0"); JButtonGroupPanel1.add(b0); b0.setFont(f); bdot.setText("."); bdot.setActionCommand("."); JButtonGroupPanel1.add(bdot); bdot.setFont(f); bdivide.setText("/"); bdivide.setActionCommand("/"); JButtonGroupPanel1.add(bdivide); bdivide.setFont(f); screen.setText("0"); screen.setHorizontalAlignment( javax.swing.SwingConstants.RIGHT); getContentPane().add(screen); screen.setFont(f); screen.setBounds(24, 24, 253, 37); bequals.setText("="); bequals.setActionCommand("="); getContentPane().add(bequals); bequals.setFont(f); bequals.setBounds(216, 348, 61, 61); //}} //{{INIT_MENUS //}} //{{REGISTER_LISTENERS // These are set to watch/listen to the mouse // and when its clicked they respond. SymMouse aSymMouse = new SymMouse(); b9.addMouseListener(aSymMouse); b8.addMouseListener(aSymMouse); b7.addMouseListener(aSymMouse); b6.addMouseListener(aSymMouse); b5.addMouseListener(aSymMouse); b4.addMouseListener(aSymMouse); b3.addMouseListener(aSymMouse); b2.addMouseListener(aSymMouse); b1.addMouseListener(aSymMouse); b0.addMouseListener(aSymMouse); bdot.addMouseListener(aSymMouse); bequals.addMouseListener(aSymMouse); badd.addMouseListener(aSymMouse); bminus.addMouseListener(aSymMouse); bmultiply.addMouseListener(aSymMouse); bdivide.addMouseListener(aSymMouse); bclear.addMouseListener(aSymMouse); //}} } public CalculatorGUI(String sTitle) { this(); setTitle(sTitle); } public void setVisible(boolean b) { if (b) setLocation(50, 50); super.setVisible(b); } static public void main(String args[]) { (new CalculatorGUI()).setVisible(true); } public void addNotify() { // Record the size of the window prior to calling parents addNotify. Dimension size = getSize(); super.addNotify(); if (frameSizeAdjusted) return; frameSizeAdjusted = true; // Adjust size of frame according to the insets and menu bar Insets insets = getInsets(); javax.swing.JMenuBar menuBar = getRootPane().getJMenuBar(); int menuBarHeight = 0; if (menuBar != null) menuBarHeight = menuBar.getPreferredSize().height; setSize(insets.left + insets.right + size.width, insets.top + insets.bottom + size.height + menuBarHeight); } // Used by addNotify boolean frameSizeAdjusted = false; //{{DECLARE_CONTROLS JPanel JButtonGroupPanel1 = new JPanel(); // com.symantec.itools.swing.JButtonGroupPanel JButtonGroupPanel1 = // new com.symantec.itools.swing.JButtonGroupPanel(); javax.swing.JButton b7 = new javax.swing.JButton(); javax.swing.JButton b8 = new javax.swing.JButton(); javax.swing.JButton b9 = new javax.swing.JButton(); javax.swing.JButton badd = new javax.swing.JButton(); javax.swing.JButton b4 = new javax.swing.JButton(); javax.swing.JButton b5 = new javax.swing.JButton(); javax.swing.JButton b6 = new javax.swing.JButton(); javax.swing.JButton bminus = new javax.swing.JButton(); javax.swing.JButton b1 = new javax.swing.JButton(); javax.swing.JButton b2 = new javax.swing.JButton(); javax.swing.JButton b3 = new javax.swing.JButton(); javax.swing.JButton bmultiply = new javax.swing.JButton(); javax.swing.JButton bclear = new javax.swing.JButton(); javax.swing.JButton b0 = new javax.swing.JButton(); javax.swing.JButton bdot = new javax.swing.JButton(); javax.swing.JButton bdivide = new javax.swing.JButton(); javax.swing.JTextField screen = new javax.swing.JTextField(); javax.swing.JButton bequals = new javax.swing.JButton(); //}} //{{DECLARE_MENUS //}} // This is an inner class that handles the events such as when the mouse is clicked // or when a key is pressed. class SymMouse extends java.awt.event.MouseAdapter { int operator_count = 0; public void mouseClicked(java.awt.event.MouseEvent event) { try { // We need an instance of the calculator to use. Calculator mycalc = new Calculator(); // What object caused the event to happen Object object = event.getSource(); // Appending numbers to screen's contents when // number buttons are selected if (object == b9) screen_text = screen_text + "9"; if (object == b8) screen_text = screen_text + "8"; if (object == b7) screen_text = screen_text + "7"; if (object == b6) screen_text = screen_text + "6"; if (object == b5) screen_text = screen_text + "5"; if (object == b4) screen_text = screen_text + "4"; if (object == b3) screen_text = screen_text + "3"; if (object == b2) screen_text = screen_text + "2"; if (object == b1) screen_text = screen_text + "1"; if (object == b0) screen_text = screen_text + "0"; if (object == bdot) screen_text = screen_text + "."; // We have a separate method for when an operator is clicked // as we need to do it again later. if ((object == badd) || (object == bminus) || (object == bmultiply) || (object == bdivide)) { screen_text += append_operator(object); operator_count++; } // Clear the contents and reset the operator counter if (object == bclear) { screen_text = ""; operator_count = 0; } // Calculate a running total if (object != bequals && operator_count >= 2) { screen_text = calculate_running_total(screen_text, mycalc); operator_count = 0; if ((object == badd) || (object == bminus) || (object == bmultiply) || (object == bdivide)) { screen_text += append_operator(object); } } // Calculate the final total when the equals button is pressed if (object == bequals) { screen_text = calculate_total(screen_text, mycalc); operator_count = 0; } // Remember to redraw the screen else nothing will happen update_screen(); } catch (Exception e) { // Just in case it goes pear-shaped, tell us what went wrong System.out.println(e.toString()); } } } // Update the box containing the numbers and operators void update_screen() { try { screen.setText(screen_text); } catch (java.lang.Exception e) { } } // A method that takes the object and returns it textual representation // The explicit cast to a button is acceptable since we're only using buttons // in this case. String append_operator(Object object) { JButton foo = (JButton) object; return foo.getText(); } // Calculate the running total by loping the last operator off the end of the line // May be required dependent on implementation of Calculator class String calculate_running_total(String calculation, Calculator mycalc) { calculation = calculation.substring(0, calculation.length() - 1) + "="; return calculate_total(calculation, mycalc); } // Generate final total // Print statements to aid with debugging of GUI, but left in. String calculate_total(String calculation, Calculator mycalc) { System.out.println("Calculation called with: " + calculation); System.out.println("Calculation returned with: " + mycalc.workOut(calculation)); Double foodle = new Double(mycalc.workOut(calculation)); return foodle.toString(); } }