GUI Simple Calculator


Hi everyone!  This is GUI coding to create a simple calculator.  It looks like the following!

It does simple mathematical calculation for multiplication, division, summation and subtraction.  It works for integers and real numbers. 

The coding of this simple calculator is as follows.  Go ahead and try by typing it or copy paste into your NetBeans  Source window.  I named my file as SimpleCalculator.java.

My coding looks like following in NetBeans Source window. I will provide you coding so that you can directly copy and paste.

Coding to copy into NetBeans;

*****************************************************************************************************************

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Calculator implements ActionListener {

    int operator = 0;

    double a = 0, b = 0, result = 0; 

    Operation obj = new Operation();

    JLabel Name, SLab;

    JButton n1, n2, n3, n4, n5, n6, n7, n8, n9, n0, clear, plus, negative, divide, multi, equal, decimal;

    JTextField Result;

    JFrame frame;

    JPanel panel;

    Font text, text1, text2;

    // Buttons

    Calculator() {

        Result = new JTextField();

        n1 = new JButton(“1”);

        n2 = new JButton(“2”);

        n3 = new JButton(“3”);

        n4 = new JButton(“4”);

        n5 = new JButton(“5”);

        n6 = new JButton(“6”);

        n7 = new JButton(“7”);

        n8 = new JButton(“8”);

        n9 = new JButton(“9”);

        n0 = new JButton(“0”);

        clear = new JButton(“Clear”);

        plus = new JButton(“+”);

        divide = new JButton(“÷”);

        multi = new JButton(“*”);

        negative = new JButton(“-“);

        equal = new JButton(“=”);

        decimal = new JButton(“.”);

        SLab = new JLabel(“”);

        //Setting fonts 

        text = new Font(“Sansserif”, Font.BOLD, 30);

        text1 = new Font(“Sansserif”, Font.BOLD, 18);

        text2 = new Font(“Sansserif”, Font.BOLD, 30);

        n1.setFont(text1);

        n2.setFont(text1);

        n3.setFont(text1);

        n4.setFont(text1);

        n5.setFont(text1);

        n6.setFont(text1);

        n7.setFont(text1);

        n8.setFont(text1);

        n9.setFont(text1);

        n0.setFont(text1);

        clear.setFont(text1);

        plus.setFont(text2);

        divide.setFont(text2);

        negative.setFont(text2);

        multi.setFont(text2);

        equal.setFont(text2);

        decimal.setFont(text1);

        Result.setFont(text);

        //Setting colors

        Result.setBackground(Color.white);

        Result.setToolTipText(“Enter The Value”);

        Result.setForeground(Color.black);

        n1.setBackground(Color.yellow);

        n2.setBackground(Color.yellow);

        n3.setBackground(Color.yellow);

        n4.setBackground(Color.yellow);

        n5.setBackground(Color.yellow);

        n6.setBackground(Color.yellow);

        n7.setBackground(Color.yellow);

        n8.setBackground(Color.yellow);

        n9.setBackground(Color.yellow);

        n0.setBackground(Color.yellow);

        decimal.setBackground(Color.yellow);

        clear.setBackground(Color.gray);

        plus.setBackground(Color.gray);

        divide.setBackground(Color.gray);

        multi.setBackground(Color.gray);

        negative.setBackground(Color.gray);

        equal.setBackground(Color.gray);

        equal.setToolTipText(“Click for Equal”);

        //Setting boundaries

        SLab.setBounds(0, 10, 100, 10);

        Result.setBounds(30, 20, 405, 120);

        n1.setBounds(30, 160, 50, 40);

        n2.setBounds(90, 160, 50, 40);

        n3.setBounds(150, 160, 50, 40);

        n4.setBounds(30, 210, 50, 40);

        n5.setBounds(90, 210, 50, 40);

        n6.setBounds(150, 210, 50, 40);

        n7.setBounds(30, 260, 50, 40);

        n8.setBounds(90, 260, 50, 40);

        n9.setBounds(150, 260, 50, 40);

        n0.setBounds(30, 310, 50, 40);

        decimal.setBounds(90, 310, 50, 40);

        clear.setBounds(30, 360, 170, 40);

        multi.setBounds(240, 160, 60, 65);

        divide.setBounds(305, 160, 60, 65);

        plus.setBounds(240, 260, 60, 65);

        negative.setBounds(305, 260, 60, 65);

        equal.setBounds(370, 160, 60, 165);

        //action

        n1.addActionListener(this);

        n2.addActionListener(this);

        n3.addActionListener(this);

        n4.addActionListener(this);

        n5.addActionListener(this);

        n6.addActionListener(this);

        n7.addActionListener(this);

        n8.addActionListener(this);

        n9.addActionListener(this);

        n0.addActionListener(this);

        decimal.addActionListener(this);

        clear.addActionListener(this);

        plus.addActionListener(this);

        divide.addActionListener(this);

        multi.addActionListener(this);

        negative.addActionListener(this);

        equal.addActionListener(this);

        //Panel

        panel = new JPanel();

        panel.setLayout(null);

        panel.setVisible(true);

        panel.setBackground(Color.LIGHT_GRAY);

        panel.add(Result);

        panel.add(n1);

        panel.add(n2);

        panel.add(n3);

        panel.add(n4);

        panel.add(n5);

        panel.add(n6);

        panel.add(n7);

        panel.add(n8);

        panel.add(n9);

        panel.add(n0);

        panel.add(decimal);

        panel.add(clear);

        panel.add(plus);

        panel.add(divide);

        panel.add(multi);

        panel.add(negative);

        panel.add(equal);

        panel.add(SLab);

        //Frame  

        frame = new JFrame(“Calculator”);

        frame.setBounds(600, 300, 460, 470);

        frame.setVisible(true);

        frame.getContentPane().add(panel);

        frame.setResizable(false);

        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent ae) {

        if (ae.getSource() == n1) {

            Result.setText(Result.getText().concat(“1”));

        } else if (ae.getSource() == n2) {

            Result.setText(Result.getText().concat(“2”));

        } else if (ae.getSource() == n3) {

            Result.setText(Result.getText().concat(“3”));

        } else if (ae.getSource() == n4) {

            Result.setText(Result.getText().concat(“4”));

        } else if (ae.getSource() == n5) {

            Result.setText(Result.getText().concat(“5”));

        } else if (ae.getSource() == n6) {

            Result.setText(Result.getText().concat(“6”));

        } else if (ae.getSource() == n7) {

            Result.setText(Result.getText().concat(“7”));

        } else if (ae.getSource() == n8) {

            Result.setText(Result.getText().concat(“8”));

        } else if (ae.getSource() == n9) {

            Result.setText(Result.getText().concat(“9”));

        } else if (ae.getSource() == n0) {

            Result.setText(Result.getText().concat(“0”));

        } else if (ae.getSource() == decimal) {

            Result.setText(Result.getText().concat(“.”));

        //

        } else if (ae.getSource() == plus) {

            a = Double.parseDouble(Result.getText());

            operator = 1;

            Result.setText(“”);

        } else if (ae.getSource() == divide) {

            a = Double.parseDouble(Result.getText());

            operator = 2;

            Result.setText(“”);

        } else if (ae.getSource() == multi) {

            a = Double.parseDouble(Result.getText());

            operator = 3;

            Result.setText(“”);

        } else if (ae.getSource() == negative) {

            a = Double.parseDouble(Result.getText());

            operator = 4;

            Result.setText(“”);

        } else if (ae.getSource() == equal) {

            b = Double.parseDouble(Result.getText());

            switch (operator) {

                case 1:

                    result = obj.Add(a, b);

                    break;

                case 2:

                    result = obj.Divide(a, b);

                    break;

                case 3:

                    result = obj.Multi(a, b);

                    break;

                case 4:

                    result = obj.Sub(a, b);

                    break;

            }

            Result.setText(“” + result);

        } else if (ae.getSource() == clear) {

            Result.setText(“”);

        }

    }

}

class Operation {

    public double Add(double input1, double input2) {

        double c = input1 + input2;

        return (c);

    }

    public double Sub(double input1, double input2) {

        double c = input1 – input2;

        return (c);

    }

    public double Multi(double input1, double input2) {

        double c = input1 * input2;

        return (c);

    }

    public double Divide(double input1, double input2) {

        double c = input1 / input2;

        return (c);

    }

}

public class SimpleCalculator {

    public static void main(String[] arg) {

        Calculator b = new Calculator();

    }

}

********************************************************************************************************

In the next post,  we will learn how to convert GUI codings into desktop applications. 

Leave a comment