Java Calculator Program with AWT and Swing Based GUI

This Java source code implements a basic calculator with a graphical user interface (GUI) using AWT and Swing. The program is structured in a way that makes it suitable for novice programmers to understand and learn about GUI development in Java. It serves as an excellent starting point for those looking to explore basic GUI applications and understand the principles of event-driven programming.

Java Calculator Program with GUI

This Java source code implements a basic calculator with a graphical user interface (GUI) using AWT and Swing. The calculator features a clear layout, displaying a large text field at the top for entering and displaying numbers. The numeric keypad, along with buttons for addition, subtraction, multiplication, division, and other functions, is arranged neatly below the text field. Users can input numbers and perform arithmetic operations by clicking the corresponding buttons. The code utilizes event handling to respond to user interactions, ensuring accurate calculations and displaying results dynamically.

The program is structured in a way that makes it suitable for novice programmers to understand and learn about GUI development in Java. It serves as an excellent starting point for those looking to explore basic GUI applications and understand the principles of event-driven programming.

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

public class CalculatorGUI extends JFrame implements ActionListener {

    // GUI components
    private JTextField textField;
    private JButton[] numberButtons;
    private JButton[] functionButtons;
    private JButton addButton, subButton, mulButton, divButton;
    private JButton clrButton, eqlButton, dotButton;

    // Variables
    private double num1, num2, result;
    private char operator;

    public CalculatorGUI() {
        // Frame setup
        setTitle("Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(5, 4));

        // Text field
        textField = new JTextField();
        textField.setFont(new Font("Arial", Font.PLAIN, 30));
        textField.setHorizontalAlignment(JTextField.RIGHT);
        add(textField);

        // Number buttons
        numberButtons = new JButton[10];
        for (int i = 0; i < 10; i++) {
            numberButtons[i] = new JButton(String.valueOf(i));
            numberButtons[i].setFont(new Font("Arial", Font.PLAIN, 20));
            numberButtons[i].addActionListener(this);
            add(numberButtons[i]);
        }

        // Function buttons
        addButton = new JButton("+");
        subButton = new JButton("-");
        mulButton = new JButton("*");
        divButton = new JButton("/");
        clrButton = new JButton("C");
        eqlButton = new JButton("=");
        dotButton = new JButton(".");

        functionButtons = new JButton[]{addButton, subButton, mulButton, divButton, clrButton, eqlButton, dotButton};

        for (JButton button : functionButtons) {
            button.setFont(new Font("Arial", Font.PLAIN, 20));
            button.addActionListener(this);
            add(button);
        }

        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < 10; i++) {
            if (e.getSource() == numberButtons[i]) {
                textField.setText(textField.getText() + i);
            }
        }

        if (e.getSource() == dotButton) {
            if (!textField.getText().contains(".")) {
                textField.setText(textField.getText() + ".");
            }
        }

        if (e.getSource() == addButton) {
            num1 = Double.parseDouble(textField.getText());
            operator = '+';
            textField.setText("");
        }

        if (e.getSource() == subButton) {
            num1 = Double.parseDouble(textField.getText());
            operator = '-';
            textField.setText("");
        }

        if (e.getSource() == mulButton) {
            num1 = Double.parseDouble(textField.getText());
            operator = '*';
            textField.setText("");
        }

        if (e.getSource() == divButton) {
            num1 = Double.parseDouble(textField.getText());
            operator = '/';
            textField.setText("");
        }

        if (e.getSource() == eqlButton) {
            num2 = Double.parseDouble(textField.getText());

            switch (operator) {
                case '+':
                    result = num1 + num2;
                    break;
                case '-':
                    result = num1 - num2;
                    break;
                case '*':
                    result = num1 * num2;
                    break;
                case '/':
                    if (num2 != 0) {
                        result = num1 / num2;
                    } else {
                        textField.setText("Error");
                        return;
                    }
                    break;
            }

            textField.setText(String.valueOf(result));
            num1 = result;
        }

        if (e.getSource() == clrButton) {
            textField.setText("");
            num1 = num2 = result = 0;
        }
    }

    public static void main(String[] args) {
        new CalculatorGUI();
    }
}
Scroll to Top