Start Search Contents Index Links About
Calculator.java
/*
 * Author: Havard Rast Blok
 * E-mail:  
 * Web   : www.rememberjava.com
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * A simple calculator.
 */
public class Calculator extends JFrame implements ActionListener
{
  // arithmetic variables
  private char savedAction;
  private boolean clear;
  private float savedValue;
   
  // GUI variables
  private JTextField display;
  private JPanel buttonsPanel;
  private JButton buttons[];
  
  public Calculator()
  {
    setupGUI();
    
    //set to 'true' to delete display at start
    clear = true;
  }
  
  private void setupGUI()
  {
    // add panels and layouts
    Container c = getContentPane();
    c.setLayoutnew BorderLayout() );
    
    display = new JTextField("0");
    display.setHorizontalAlignment(JTextField.TRAILING);
    c.add(display, BorderLayout.NORTH);
    
    buttonsPanel = new JPanel();
    buttonsPanel.setLayoutnew GridLayout(4,4) );
    c.addbuttonsPanel, BorderLayout.CENTER );
    buttons = new JButton[16];
    
    // make the buttons, set ActionListener and add to panel
    for(int i=0; i<buttons.length; i++)
    {
      buttons[inew JButton();
      buttons[i].addActionListenerthis );
      buttonsPanel.addbuttons[i] );
    }
    
    // set the number captions
    buttons[0].setText("7");
    buttons[1].setText("8");
    buttons[2].setText("9");
    buttons[4].setText("4");
    buttons[5].setText("5");
    buttons[6].setText("6");
    buttons[8].setText("1");
    buttons[9].setText("2");
    buttons[10].setText("3");
    buttons[12].setText("0");
    
    // set other captions
    buttons[3].setText("/");
    buttons[7].setText("*");
    buttons[11].setText("-");
    buttons[15].setText("+");
    buttons[13].setText(".");
    buttons[14].setText("=");
    
    pack();
  }
  
  public void actionPerformedActionEvent e )
  {
    String str;
    char cmd;
    
    // get caption from button and convert to char
    str = e.getActionCommand();
    cmd = (str.toCharArray())[0];
    
    // display or calulate
    ifCharacter.isDigitcmd || cmd == '.' )
    {
      ifclear )
      {
        // save current value and clear the display
        savedValue = Float.parseFloatdisplay.getText() );
        display.setText("");
        clear = false;
      }
      
      // add new digit to display
      display.setTextdisplay.getText() + cmd );
    }
    else
    {
      calculate(cmd);
    }
  }
  
  private void calculate(char cmd)
  {
    float curValue;
    float newValue = 0;
    
    // get current value of display
    curValue = Float.parseFloatdisplay.getText() );
    
    ifsavedAction == )
    {
      // if no previous action was save, save this one and clear
      clear = true;
      savedAction = cmd;
    }
    else
    {
      // perform the previous action
      switch(savedAction)
      {
        case '/':
                  newValue = savedValue / curValue;
                  break;
        case '*':
                  newValue = savedValue * curValue;
                  break;
        case '-':
                  newValue = savedValue - curValue;
                  break;
        case '+':
                  newValue = savedValue + curValue;
                  break;                  
      }
      
      // display the result and order to clear
      display.setText(""+newValue);
      clear = true;
      
      ifcmd == '=' )
      {
        //do not save = action
        savedAction = 0;
      }
      else
      {
        // save other actions
        savedAction = cmd;
      }
    }

  }

  public static void main(String args[])
  {
    new Calculator().show();
  }
  

  
}


site: Håvard Rast Blok
mail:
updated: 27 July 2007