Swing GUI Calculator in Java

Swing GUI Calculator in Java

Hello everyone 👋

In this article, we will create a simple Swing GUI Calculator in Java having basic functionalities, but once you are familiar with the core logic of the entire program, you can easily add more functionalities to the application and make it your own. Let’s start

Project Overview: Swing GUI Calculator in Java

Project Name:Swing GUI Calculator in Java
AbstractIt’s a GUI-based project used with the Swing module to organize all the elements that work under the Calculator in Java.
Language/s Used:Java
IDEIntelliJ Idea Professional(Recommended)
Java version (Recommended):Java SE 18.0. 2.1
Database:No need
Type:Desktop Application
Recommended forIntermediates of Java
Time needed for project1.5 – 2.5 hours

What you will learn after creating Swing GUI Calculator in Java?

  • Math class of Java which is very useful for performing arithmetic calculations on numbers.
  • Handling Classes and Objects creations
  • Functions, Loops, Conditionals, and variables
  • Java Swing and Java AWT for creating a user-friendly UI.

Features of Project:

  • Addition, Subtraction, Multiplication, and Division
  • Finding the square, square root, and reciprocal of a number

Create a file with the name Calculator.java

folder structure

We have used the Math class of java to provide all the functionalities like Addition, Subtraction, Multiplication, Division, finding squares, square roots, and the reciprocal of a number. 

You can add more functionalities like finding Sin, Cos, Tan, and Log of numbers. Refer to the Math Class in Java for more info.

Now, we will understand the code for Swing GUI Calculator in Java, we will understand each code using comments.

Complete Code for Swing GUI Calculator in Java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
class Calculator implements ActionListener {
 
    double number, answer;
    int calculation;
 
   
    JFrame frame;
    JLabel label = new JLabel();
    JTextField textField = new JTextField();
    JRadioButton onRadioButton = new JRadioButton("on");
    JRadioButton offRadioButton = new JRadioButton("off");
    JButton buttonZero = new JButton("0");
    JButton buttonOne = new JButton("1");
    JButton buttonTwo = new JButton("2");
    JButton buttonThree = new JButton("3");
    JButton buttonFour = new JButton("4");
    JButton buttonFive = new JButton("5");
    JButton buttonSix = new JButton("6");
    JButton buttonSeven = new JButton("7");
    JButton buttonEight = new JButton("8");
    JButton buttonNine = new JButton("9");
    JButton buttonDot = new JButton(".");
    JButton buttonClear = new JButton("C");
    JButton buttonDelete = new JButton("DEL");
    JButton buttonEqual = new JButton("=");
    JButton buttonMul = new JButton("x");
    JButton buttonDiv = new JButton("/");
    JButton buttonPlus = new JButton("+");
    JButton buttonMinus = new JButton("-");
    JButton buttonSquare = new JButton("x\u00B2");
    JButton buttonReciprocal = new JButton("1/x");
    JButton buttonSqrt = new JButton("\u221A");
   
    // Colors for the UI
    Color pink = new Color(239, 71, 111);
    Color yellow = new Color(255, 209, 102);
    Color green = new Color(6, 214, 160);
    Color pastel = new Color(7, 59, 76);
 
    Calculator() {
        prepareGUI();
        addComponents();
        addActionEvent();
    }
 
    // Method to prepare the GUI
    public void prepareGUI() {
        frame = new JFrame();
        frame.setTitle("Calculator");
        frame.setSize(300, 490);
        frame.getContentPane().setLayout(null);
        frame.getContentPane().setBackground(yellow);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    // Method to add all the components like the numbers and symbols of all the operations to the GUI
    public void addComponents() {
        label.setBounds(250, 0, 50, 50);
        label.setForeground(Color.black);
        frame.add(label);
 
        textField.setBounds(10, 40, 270, 40);
        textField.setFont(new Font("Arial", Font.BOLD, 20));
        textField.setEditable(false);
        textField.setHorizontalAlignment(SwingConstants.RIGHT);
        frame.add(textField);
 
        onRadioButton.setBounds(10, 95, 60, 40);
        onRadioButton.setSelected(true);
        onRadioButton.setFont(new Font("Arial", Font.BOLD, 14));
        onRadioButton.setBackground(yellow);
        onRadioButton.setForeground(Color.black);
        frame.add(onRadioButton);
 
        offRadioButton.setBounds(10, 120, 60, 40);
        offRadioButton.setSelected(false);
        offRadioButton.setFont(new Font("Arial", Font.BOLD, 14));
        offRadioButton.setBackground(yellow);
        offRadioButton.setForeground(Color.black);
        frame.add(offRadioButton);
 
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(onRadioButton);
        buttonGroup.add(offRadioButton);
 
        buttonSeven.setBounds(10, 230, 60, 40);
        buttonSeven.setFont(new Font("Arial", Font.BOLD, 20));
        buttonSeven.setBackground(pastel);
        buttonSeven.setForeground(Color.white);
        frame.add(buttonSeven);
 
        buttonEight.setBounds(80, 230, 60, 40);
        buttonEight.setFont(new Font("Arial", Font.BOLD, 20));
        buttonEight.setBackground(pastel);
        buttonEight.setForeground(Color.white);
        frame.add(buttonEight);
 
        buttonNine.setBounds(150, 230, 60, 40);
        buttonNine.setFont(new Font("Arial", Font.BOLD, 20));
        buttonNine.setBackground(pastel);
        buttonNine.setForeground(Color.white);
        frame.add(buttonNine);
 
        buttonFour.setBounds(10, 290, 60, 40);
        buttonFour.setFont(new Font("Arial", Font.BOLD, 20));
        buttonFour.setBackground(pastel);
        buttonFour.setForeground(Color.white);
        frame.add(buttonFour);
 
        buttonFive.setBounds(80, 290, 60, 40);
        buttonFive.setFont(new Font("Arial", Font.BOLD, 20));
        buttonFive.setBackground(pastel);
        buttonFive.setForeground(Color.white);
        frame.add(buttonFive);
 
        buttonSix.setBounds(150, 290, 60, 40);
        buttonSix.setFont(new Font("Arial", Font.BOLD, 20));
        buttonSix.setBackground(pastel);
        buttonSix.setForeground(Color.white);
        frame.add(buttonSix);
 
        buttonOne.setBounds(10, 350, 60, 40);
        buttonOne.setFont(new Font("Arial", Font.BOLD, 20));
        buttonOne.setBackground(pastel);
        buttonOne.setForeground(Color.white);
        frame.add(buttonOne);
 
        buttonTwo.setBounds(80, 350, 60, 40);
        buttonTwo.setFont(new Font("Arial", Font.BOLD, 20));
        buttonTwo.setBackground(pastel);
        buttonTwo.setForeground(Color.white);
        frame.add(buttonTwo);
 
        buttonThree.setBounds(150, 350, 60, 40);
        buttonThree.setFont(new Font("Arial", Font.BOLD, 20));
        buttonThree.setBackground(pastel);
        buttonThree.setForeground(Color.white);
        frame.add(buttonThree);
 
        buttonDot.setBounds(150, 410, 60, 40);
        buttonDot.setFont(new Font("Arial", Font.BOLD, 20));
        buttonDot.setBackground(pastel);
        buttonDot.setForeground(Color.white);
        frame.add(buttonDot);
 
        buttonZero.setBounds(10, 410, 130, 40);
        buttonZero.setFont(new Font("Arial", Font.BOLD, 20));
        buttonZero.setBackground(pastel);
        buttonZero.setForeground(Color.white);
        frame.add(buttonZero);
 
        buttonEqual.setBounds(220, 350, 60, 100);
        buttonEqual.setFont(new Font("Arial", Font.BOLD, 20));
        buttonEqual.setBackground(green);
        frame.add(buttonEqual);
 
        buttonDiv.setBounds(220, 110, 60, 40);
        buttonDiv.setFont(new Font("Arial", Font.BOLD, 20));
        buttonDiv.setBackground(green);
        frame.add(buttonDiv);
 
        buttonSqrt.setBounds(10, 170, 60, 40);
        buttonSqrt.setFont(new Font("Arial", Font.BOLD, 18));
        buttonSqrt.setBackground(green);
        frame.add(buttonSqrt);
 
        buttonMul.setBounds(220, 230, 60, 40);
        buttonMul.setFont(new Font("Arial", Font.BOLD, 20));
        buttonMul.setBackground(green);
        frame.add(buttonMul);
 
        buttonMinus.setBounds(220, 170, 60, 40);
        buttonMinus.setFont(new Font("Arial", Font.BOLD, 20));
        buttonMinus.setBackground(green);
        frame.add(buttonMinus);
 
        buttonPlus.setBounds(220, 290, 60, 40);
        buttonPlus.setFont(new Font("Arial", Font.BOLD, 20));
        buttonPlus.setBackground(green);
        frame.add(buttonPlus);
 
        buttonSquare.setBounds(80, 170, 60, 40);
        buttonSquare.setFont(new Font("Arial", Font.BOLD, 20));
        buttonSquare.setBackground(green);
        frame.add(buttonSquare);
 
        buttonReciprocal.setBounds(150, 170, 60, 40);
        buttonReciprocal.setFont(new Font("Arial", Font.BOLD, 15));
        buttonReciprocal.setBackground(green);
        frame.add(buttonReciprocal);
 
        buttonDelete.setBounds(150, 110, 60, 40);
        buttonDelete.setFont(new Font("Arial", Font.BOLD, 12));
        buttonDelete.setBackground(pink);
        buttonDelete.setForeground(Color.white);
        frame.add(buttonDelete);
 
        buttonClear.setBounds(80, 110, 60, 40);
        buttonClear.setFont(new Font("Arial", Font.BOLD, 12));
        buttonClear.setBackground(pink);
        buttonClear.setForeground(Color.white);
        frame.add(buttonClear);
 
    }
 
    // Method to add all the action listeners to the buttons
    public void addActionEvent() {
        onRadioButton.addActionListener(this);
        offRadioButton.addActionListener(this);
        buttonClear.addActionListener(this);
        buttonDelete.addActionListener(this);
        buttonDiv.addActionListener(this);
        buttonSqrt.addActionListener(this);
        buttonSquare.addActionListener(this);
        buttonReciprocal.addActionListener(this);
        buttonMinus.addActionListener(this);
        buttonSeven.addActionListener(this);
        buttonEight.addActionListener(this);
        buttonNine.addActionListener(this);
        buttonMul.addActionListener(this);
        buttonFour.addActionListener(this);
        buttonFive.addActionListener(this);
        buttonSix.addActionListener(this);
        buttonPlus.addActionListener(this);
        buttonOne.addActionListener(this);
        buttonTwo.addActionListener(this);
        buttonThree.addActionListener(this);
        buttonEqual.addActionListener(this);
        buttonZero.addActionListener(this);
        buttonDot.addActionListener(this);
    }
 
    // Method to add all the key listeners to the buttons and the frame
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
 
        // If the on radio button is selected, the calculator is enabled
        if (source == onRadioButton) {
            enable();
        }
        // If the off radio button is selected, the calculator is disabled
        else if (source == offRadioButton) {
            disable();
        }
        // If the clear button is pressed, the text field is cleared
        else if (source == buttonClear) {
            label.setText("");
            textField.setText("");
        }
        // If the delete button is pressed, the last character is deleted
        else if (source == buttonDelete) {
            int length = textField.getText().length(); // Get the length of the text field
            int number = length - 1; // Get the number of characters to delete
 
 
            if (length > 0) { // If the length is greater than 0, delete the last character
                StringBuilder back = new StringBuilder(textField.getText());
                back.deleteCharAt(number);
                textField.setText(back.toString());
 
            }
            if (textField.getText().endsWith("")) { // If the text field is empty, set the label to 0
                label.setText("");
            }
        } else if (source == buttonZero) { // If the zero button is pressed, add a zero to the text field
            if (textField.getText().equals("0")) { // If the text field is 0, do nothing
                return;
            } else { // If the text field is not empty, add a zero to the text field
                textField.setText(textField.getText() + "0");
            }
        }
        // If the one button is pressed, add a one to the text field and so on for all the other buttons
        else if (source == buttonOne) {  
            textField.setText(textField.getText() + "1");
        } else if (source == buttonTwo) {
            textField.setText(textField.getText() + "2");
        } else if (source == buttonThree) {
            textField.setText(textField.getText() + "3");
        } else if (source == buttonFour) {
            textField.setText(textField.getText() + "4");
        } else if (source == buttonFive) {
            textField.setText(textField.getText() + "5");
        } else if (source == buttonSix) {
            textField.setText(textField.getText() + "6");
        } else if (source == buttonSeven) {
            textField.setText(textField.getText() + "7");
        } else if (source == buttonEight) {
            textField.setText(textField.getText() + "8");
        } else if (source == buttonNine) {
            textField.setText(textField.getText() + "9");
        } else if (source == buttonDot) {
            if (textField.getText().contains(".")) { // If the text field contains a dot, do nothing
                return;
            } else {
                textField.setText(textField.getText() + "."); // If the text field does not contain a dot, add a dot
            }
 
        }
        // If the plus button is pressed, add a plus to the text field and so on for all the buttons
        else if (source == buttonPlus) {
            String str = textField.getText();
            number = Double.parseDouble(textField.getText());
            textField.setText("");
            label.setText(str + "+");
            calculation = 1;
        } else if (source == buttonMinus) {
            String str = textField.getText();
            number = Double.parseDouble(textField.getText());
            textField.setText("");
            label.setText(str + "-");
            calculation = 2;
        } else if (source == buttonMul) {
            String str = textField.getText();
            number = Double.parseDouble(textField.getText());
            textField.setText("");
            label.setText(str + "X");
            calculation = 3;
        } else if (source == buttonDiv) {
            String str = textField.getText();
            number = Double.parseDouble(textField.getText());
            textField.setText("");
            label.setText(str + "/");
            calculation = 4;
        } else if (source == buttonSqrt) {
            number = Double.parseDouble(textField.getText());
            Double sqrt = Math.sqrt(number);
            textField.setText(Double.toString(sqrt));
 
        } else if (source == buttonSquare) {
            String str = textField.getText();
            number = Double.parseDouble(textField.getText());
            double square = Math.pow(number, 2);
            String string = Double.toString(square);
            if (string.endsWith(".0")) {
                textField.setText(string.replace(".0", ""));
            } else {
                textField.setText(string);
            }
            label.setText("(sqr)" + str);
        } else if (source == buttonReciprocal) {
            number = Double.parseDouble(textField.getText());
            double reciprocal = 1 / number;
            String string = Double.toString(reciprocal);
            if (string.endsWith(".0")) {
                textField.setText(string.replace(".0", ""));
            } else {
                textField.setText(string);
            }
        } else if (source == buttonEqual) {
            switch (calculation) {
                case 1:
                    answer = number + Double.parseDouble(textField.getText());
                    if (Double.toString(answer).endsWith(".0")) {
                        textField.setText(Double.toString(answer).replace(".0", ""));
                    } else {
                        textField.setText(Double.toString(answer));
                    }
                    label.setText("");
                    break;
                case 2:
                    answer = number - Double.parseDouble(textField.getText());
                    if (Double.toString(answer).endsWith(".0")) {
                        textField.setText(Double.toString(answer).replace(".0", ""));
                    } else {
                        textField.setText(Double.toString(answer));
                    }
                    label.setText("");
                    break;
                case 3:
                    answer = number * Double.parseDouble(textField.getText());
                    if (Double.toString(answer).endsWith(".0")) {
                        textField.setText(Double.toString(answer).replace(".0", ""));
                    } else {
                        textField.setText(Double.toString(answer));
                    }
                    label.setText("");
                    break;
                case 4:
                    answer = number / Double.parseDouble(textField.getText());
                    if (Double.toString(answer).endsWith(".0")) {
                        textField.setText(Double.toString(answer).replace(".0", ""));
                    } else {
                        textField.setText(Double.toString(answer));
                    }
                    label.setText("");
                    break;
 
            }
        }
 
 
    }
 
    // method to enable the calculator
    public void enable() {
        onRadioButton.setEnabled(false);
        offRadioButton.setEnabled(true);
        textField.setEnabled(true);
        label.setEnabled(true);
        buttonClear.setEnabled(true);
        buttonDelete.setEnabled(true);
        buttonDiv.setEnabled(true);
        buttonSqrt.setEnabled(true);
        buttonSquare.setEnabled(true);
        buttonReciprocal.setEnabled(true);
        buttonMinus.setEnabled(true);
        buttonSeven.setEnabled(true);
        buttonEight.setEnabled(true);
        buttonNine.setEnabled(true);
        buttonMul.setEnabled(true);
        buttonFour.setEnabled(true);
        buttonFive.setEnabled(true);
        buttonSix.setEnabled(true);
        buttonPlus.setEnabled(true);
        buttonOne.setEnabled(true);
        buttonTwo.setEnabled(true);
        buttonThree.setEnabled(true);
        buttonEqual.setEnabled(true);
        buttonZero.setEnabled(true);
        buttonDot.setEnabled(true);
 
    }
 
    // method to disable the calculator
    public void disable() {
        onRadioButton.setEnabled(true);
        offRadioButton.setEnabled(false);
        textField.setText("");
        label.setText(" ");
        buttonClear.setEnabled(false);
        buttonDelete.setEnabled(false);
        buttonDiv.setEnabled(false);
        buttonSqrt.setEnabled(false);
        buttonSquare.setEnabled(false);
        buttonReciprocal.setEnabled(false);
        buttonMinus.setEnabled(false);
        buttonSeven.setEnabled(false);
        buttonEight.setEnabled(false);
        buttonNine.setEnabled(false);
        buttonMul.setEnabled(false);
        buttonFour.setEnabled(false);
        buttonFive.setEnabled(false);
        buttonSix.setEnabled(false);
        buttonPlus.setEnabled(false);
        buttonOne.setEnabled(false);
        buttonTwo.setEnabled(false);
        buttonThree.setEnabled(false);
        buttonEqual.setEnabled(false);
        buttonZero.setEnabled(false);
        buttonDot.setEnabled(false);
    }
 
    public static void main(String[] args)
    {
            new Calculator(); // create a new Calculator object
    }
}

Output:

Now, hit the run button near the top of the main method or go to the left top corner and find the run button to run the Swing GUI Calculator in Java. And here is the output

output 1 for Swing GUI Calculator in Java

ON and OFF switches are provided to turn on the calculator and turn it off when not in use. When it’s off all the buttons are disabled.

output 2 for Swing GUI Calculator in Java

Congratulations!! We just created a simple but effective Swing GUI Calculator in Java. Try adding more functionalities to your application and have fun with it.


Also Read:

Share:

Author: Ayush Purawr