Java Swing Calculator

By using a calculator we can calculate arithmetic operations(mathmetical computations) on numbers .Arithmetic operations means addition, subtraction,multiplication, divison etc.

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

//Calculator Class

public class capp extends JFrame implements ActionListener

{

   public static final int WIDTH = 200;

   public static final int HEIGHT = 270;

     private JTextField inOutField;

     private String action = "blank";

     private double value1 = 0;

   private double value2 = 0;

   public static void main(String[] args)

       {

       capp calc = new capp();

       calc.setVisible(true);

       calc.setResizable(false);

   }

   public capp()//constructor

   {

       setSize(WIDTH, HEIGHT);//frame size

       setTitle("Calculator");

       Container contentPane = getContentPane();

       contentPane.setBackground(Color.lightGray );

       contentPane.setLayout(new FlowLayout());

       JPanel textPanel = new JPanel();

       textPanel.setBackground(Color.black);

       textPanel.setLayout(new FlowLayout());

       inOutField = new JTextField(12);

       inOutField.setBackground(Color.lightGray);

       textPanel.add(inOutField);

       contentPane.add(textPanel, BorderLayout.CENTER);

       JButton buttonOne = new JButton("1");// 1 button

       buttonOne.addActionListener(this);

       contentPane.add(buttonOne); //add button1

      

       JButton buttonTwo = new JButton("2"); //2 button

       buttonTwo.addActionListener(this);

       contentPane.add(buttonTwo); //add button2

    

      

       JButton buttonThree = new JButton("3"); // button3

       buttonThree.addActionListener(this); //action performed on button3

      contentPane.add(buttonThree); //add button3

      

       JButton buttonFour = new JButton("4"); //button4

       buttonFour.addActionListener(this);

       contentPane.add(buttonFour);

      

       JButton buttonFive = new JButton("5");// button5

       buttonFive.addActionListener(this);

       contentPane.add(buttonFive);

      

       JButton buttonSix = new JButton("6");// button6

       buttonSix.addActionListener(this);

       contentPane.add(buttonSix);

              

       JButton buttonSeven = new JButton("7");// button7

       buttonSeven.addActionListener(this);

       contentPane.add(buttonSeven);

      

       JButton buttonEight = new JButton("8");// button8

       buttonEight.addActionListener(this);

       contentPane.add(buttonEight);

      

       JButton buttonNine = new JButton("9");// button9

       buttonNine.addActionListener(this);

       contentPane.add(buttonNine);

      

       JButton buttonZero = new JButton("0");//button 0(zero)

       buttonZero.addActionListener(this);//action performed on button 0

       contentPane.add(buttonZero);

      

      JButton buttonDec = new JButton(".");

       buttonDec.addActionListener(this);

       contentPane.add(buttonDec);

      

       JButton buttonClear = new JButton("C");// C for clear Button

       buttonClear.addActionListener(this);

       contentPane.add(buttonClear);

      

       JButton buttonAdd = new JButton("+");

       buttonAdd.addActionListener(this);

       contentPane.add(buttonAdd);

      

       JButton buttonSub = new JButton("-");

       buttonSub.addActionListener(this);

       contentPane.add(buttonSub);

      

       JButton buttonMult = new JButton("*");

       buttonMult.addActionListener(this);

       contentPane.add(buttonMult);

      

       JButton buttonDiv = new JButton("/");

       buttonDiv.addActionListener(this);

       contentPane.add(buttonDiv);

      

       JButton buttonEqu = new JButton("=");

       buttonEqu.addActionListener(this);

       contentPane.add(buttonEqu);

   }

// action performed when each button is pressed

   public void actionPerformed(ActionEvent ace)

   {

       String currentText = inOutField.getText();

       Container contentPane = getContentPane();

//if we click on button = then this action performed

           if(ace.getActionCommand().equals("="))

           {

                 value2 = Double.parseDouble(currentText);

                 inOutField.setText("");    

                

                 if(action.equals("addition"))//for addition

                 {

                       action = "blank";

                       inOutField.setText(value1 + value2 + "");

                 }

                 else if(action.equals("subtraction"))

                 {

                       action = "blank";

                       inOutField.setText(value1 - value2 + "");

                 }

                 else if(action.equals("multiplication"))

                 {

                       action = "blank";

                       inOutField.setText(value1 * value2 + "");

                 }

                 else if(action.equals("diviison"))

                 {

                      action = "blank";

                       inOutField.setText(value1 / value2 + "");

                 }

                

                

                 else

                 {

                       if(currentText.length() > 0)

                       inOutField.setText(currentText);

                       else

                       inOutField.setText("");//for clear the textfield

                 }

                

           }

           else if(ace.getActionCommand().equals("+"))

           {

                 action = "add";

                 value1 = Double.parseDouble(currentText);

                 inOutField.setText("");    

           }

           else if(ace.getActionCommand().equals("-"))

           {

                 action = "sub";

                 value1 = Double.parseDouble(currentText);

                 inOutField.setText("");    

           }

           else if(ace.getActionCommand().equals("*"))

         {

                 action = "mult";

                 value1 = Double.parseDouble(currentText);

                 inOutField.setText("");    

           }

           else if(ace.getActionCommand().equals("/"))

           {

                action = "div";

                 value1 = Double.parseDouble(currentText);

                 inOutField.setText("");    

           }

           else if(ace.getActionCommand().equals("C"))

           {

                 inOutField.setText("");    

           }

      

           else if(action.equals("."))

                 {

                       action = "blank";

                       inOutField.setText(currentText+=".");

                 }

    

         else

             inOutField.setText(currentText += ace.getActionCommand());

              

   }

}

calculator in java swing