This post is about creating a Standard Java Calculator application with Java Swing from the Scratch without using the eval function.

this calculator has Log that stores Your session history with their respective answer.

The first we need to do is to set package for our Java File and then import he appropriate libraries.

in our case we import awt,ActionEvent,ActionListner,Swing and BorderFactory which are the only thing we need to import.

package latestcalcinterface;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import static javax.swing.BorderFactory.createEmptyBorder;


After we have done importing our libraries we will define the Buttons, and TextArias we will be using to communicate with our user.

    JFrame CalcFrame          = new JFrame("Razor Studios");
    JTextArea DisplayArea     = new JTextArea("");
    JTextArea ResultArea      = new JTextArea("");
    JTextArea LogArea         = new JTextArea("Session History");
    JScrollPane ScrollPane    = new JScrollPane();
    JButton OperatorArea      = new JButton("");
    String TotalValueHidden   = "";
    String LastOperatorHidden = "";
    
    JButton Zero     = new JButton("0");
    JButton One      = new JButton("1");
    JButton Two      = new JButton("2");
    JButton Three    = new JButton("3");
    JButton Four     = new JButton("4");
    JButton Five     = new JButton("5");
    JButton Six      = new JButton("6");
    JButton Seven    = new JButton("7");
    JButton Eight    = new JButton("8");
    JButton Nine     = new JButton("9");
    JButton Multiply = new JButton("*");
    JButton Divide   = new JButton("/");
    JButton Minus    = new JButton("-");
    JButton Add      = new JButton("+");
    JButton Equals   = new JButton("=");
    JButton Dot      = new JButton(".");
    JButton Clear    = new JButton("Clear");
    JButton Back     = new JButton("Back");
    JButton Log      = new JButton("Log");
    JButton Return   = new JButton("<---");

After we define our variables the next thing we wanna do is to set their boundary or the place the will be sitting on and some other properties like Border, Font, Background, TextOrientation of the above components.


        CalcFrame.setLayout(null);
        CalcFrame.setSize(245,325);
        ResultArea.setBounds(0,0,246,45);
        DisplayArea.setBounds(0,45,216,45);
        OperatorArea.setBounds(216,45,30,45);
        DisplayArea.setBackground(Color.GRAY);
        DisplayArea.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        DisplayArea.setEnabled(false);
        DisplayArea.setFont(new java.awt.Font("Comic Sans MS Italic", 3, 24));
        
        LogArea.setBounds(0,0,249,315);
        LogArea.setBackground(Color.GRAY);
        LogArea.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        LogArea.setEnabled(false);
        LogArea.setFont(new java.awt.Font("Comic Sans MS Italic", 3, 15));
        
        ScrollPane.setBounds(0, 30, 249, 297);
        ScrollPane.setViewportView(LogArea);
        
        Return.setVisible(false);
        
        ResultArea.setBackground(Color.GRAY);
        ResultArea.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        ResultArea.setEnabled(false);
        ResultArea.setFont(new java.awt.Font("Comic Sans MS Italic", 3, 24));
        
        OperatorArea.setForeground(Color.GREEN);
        OperatorArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        OperatorArea.setBackground(Color.GRAY);
        OperatorArea.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        OperatorArea.setEnabled(false);
        OperatorArea.setFont(new java.awt.Font("Comic Sans MS Italic", 3, 24));

The Next thing we will be doing is to make some adjustment to our components and add all of them to the main Frame of the app CalcFrame.
        //Remove Focus From Buttons
        RemoveFocus();
        //Set BackGround For Buttons
        SetBackground();
        //Change The Text Font For The Buttons
        ChangeFont();
        //Remove Border From Buttons
        SetBorder();

        CalcFrame.add(ResultArea);      //TextArea To Display The Result
        CalcFrame.add(DisplayArea);     //TextArea To Display The Typed Data
        CalcFrame.add(OperatorArea);    //TextArea To Display The Last Clicked Operator
        CalcFrame.add(ScrollPane);
        ScrollPane.setVisible(false);
        

After we have add the Display,Result,Operator area we need to add the button that trigger the action to happen

        Seven.setBounds(5, 100, 55, 40);
        Eight.setBounds(65,100,55,40);
        Nine.setBounds(125,100,55,40);
        Divide.setBounds(185,100,55,40);
        
        //Adding Buttons To The Frame
        CalcFrame.add(Seven);     
        CalcFrame.add(Eight);
        CalcFrame.add(Nine);
        CalcFrame.add(Divide);
        
        /*
        The Second Number Rows {4,5,6,Multiply}
        Set The X-Axis, Y-Axis, Width, and Height For The Buttons
        */
        Four.setBounds(5, 145, 55, 40);
        Five.setBounds(65,145,55,40);
        Six.setBounds(125,145,55,40);
        Multiply.setBounds(185,145,55,40);
        
        //Adding Buttons To The Frame
        CalcFrame.add(Four);
        CalcFrame.add(Five);
        CalcFrame.add(Six);
        CalcFrame.add(Multiply);
        
        /* 
        The Third Number Rows {1,2,3,Minus}
        Set The X-Axis, Y-Axis, Width, and Height For The Buttons
        */
        One.setBounds(5, 190, 55, 40);
        Two.setBounds(65,190,55,40);
        Three.setBounds(125,190,55,40);
        Minus.setBounds(185,190,55,40);
        
        //Adding Elements To The Frame
        CalcFrame.add(One);
        CalcFrame.add(Two);
        CalcFrame.add(Three);
        CalcFrame.add(Minus);
        
        /* 
        The Forth Number Rows {.,0,=,+}
        Set The X-Axis, Y-Axis, Width, and Height For The Buttons
        */
        Dot.setBounds(5, 235, 55, 40);
        Zero.setBounds(65,235,55,40);
        Equals.setBounds(125,235,55,40);
        Add.setBounds(185,235,55,40);
        
        //Adding Elements To The Frame
        CalcFrame.add(Dot);
        CalcFrame.add(Zero);
        CalcFrame.add(Equals);
        CalcFrame.add(Add);
        
        //Clear And Back Buttons
        Clear.setBounds(5,280,85,40);
        Back.setBounds(95,280,85,40);
        Log.setBounds(185,280,55,40);
        Return.setBounds(0,0,80,30);
        
        CalcFrame.add(Clear);
        CalcFrame.add(Back);
        CalcFrame.add(Log);
        CalcFrame.add(Return);
        
        //Remove Border From All Buttons
        
        //Make The Frame Visible
        CalcFrame.setResizable(true);
        CalcFrame.setVisible(true);

Now we have added the components but right now they can not do anything unless we create some action for them to operate as we wanted.

So the first thing we need to do is to set action listeners for every numeric button we have so that when we click on them we can get the number printed in the DisplayArea.

For every numeric button we have, we will add action listener like below

        Zero.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if(!"0".equals(DisplayArea.getText())) {
                    DisplayArea.setText(DisplayArea.getText() + "0");
                }
            }
        });
        

After this, we have the OperatorFunction which will be triggered when we click on the +, -,x, and / buttons.


Finally we add action listeners for the Operators we have.

        Multiply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                OperatorFunction("*");
            }
        });
        
        Divide.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                OperatorFunction("/");
            } 
        });
        
        Minus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                OperatorFunction("-");
            }
        });
        
        Add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt){
                OperatorFunction("+");
           }
        });