Create a Calculator in Java

In this tutorial, we'll learn how to create a calculator in Java that looks like the one below:

Create Calculator in Java

Java provides Swing framework which is a graphical user interface framework that includes a set of classes that are powerful and flexible.

To create a calculator, we will use JFrame, JLabel, and JButton classes from the Swing framework. The JFrame class is used to construct a top-level window for a Java application.

The following code creates and displays a calculator in Java:


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Calculator implements ActionListener {

	JTextField calcScreen = new JTextField();
	double result = 0;
	char operator = 0;
	StringBuilder inputDisplayBuilder = new StringBuilder();
	StringBuilder inputNum1 = new StringBuilder();
	StringBuilder inputNum2 = new StringBuilder();

	public void show() {

		// create window for calculator
		JFrame calcFrame = new JFrame("Calculator");
		calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// create label to print time
		JLabel calcLabel = new JLabel("", SwingConstants.CENTER);
		calcLabel.setPreferredSize(new Dimension(390, 400));
		calcFrame.getContentPane().add(calcLabel, BorderLayout.CENTER);

		calcScreen.setBackground(Color.WHITE);
		calcScreen.setBounds(20, 50, 350, 50);
		calcScreen.setFont(new Font("Arial", Font.BOLD, 30));
		calcLabel.add(calcScreen);

		// first row
		JButton b7 = new JButton();
		b7.setText("7");
		b7.setBounds(20, 120, 80, 40);
		b7.addActionListener(this);
		b7.setVisible(true);
		calcLabel.add(b7);

		JButton b8 = new JButton();
		b8.setText("8");
		b8.setBounds(110, 120, 80, 40);
		b8.addActionListener(this);
		b8.setVisible(true);
		calcLabel.add(b8);

		JButton b9 = new JButton();
		b9.setText("9");
		b9.setBounds(200, 120, 80, 40);
		b9.addActionListener(this);
		b9.setVisible(true);
		calcLabel.add(b9);

		JButton aDiv = new JButton();
		aDiv.setText("÷");
		aDiv.setActionCommand("divide");
		aDiv.setBounds(290, 120, 80, 40);
		aDiv.addActionListener(this);
		aDiv.setVisible(true);
		calcLabel.add(aDiv);

		// second row
		JButton b4 = new JButton();
		b4.setText("4");
		b4.setBounds(20, 170, 80, 40);
		b4.addActionListener(this);
		b4.setVisible(true);
		calcLabel.add(b4);

		JButton b5 = new JButton();
		b5.setText("5");
		b5.setBounds(110, 170, 80, 40);
		b5.addActionListener(this);
		b5.setVisible(true);
		calcLabel.add(b5);

		JButton b6 = new JButton();
		b6.setText("6");
		b6.setBounds(200, 170, 80, 40);
		b6.addActionListener(this);
		b6.setVisible(true);
		calcLabel.add(b6);

		JButton bMulti = new JButton();
		bMulti.setText("×");
		bMulti.setActionCommand("multiply");
		bMulti.setBounds(290, 170, 80, 40);
		bMulti.addActionListener(this);
		bMulti.setVisible(true);
		calcLabel.add(bMulti);

		// third row
		JButton b1 = new JButton();
		b1.setText("1");
		b1.setBounds(20, 220, 80, 40);
		b1.addActionListener(this);
		b1.setVisible(true);
		calcLabel.add(b1);

		JButton b2 = new JButton();
		b2.setText("2");
		b2.setBounds(110, 220, 80, 40);
		b2.addActionListener(this);
		b2.setVisible(true);
		calcLabel.add(b2);

		JButton b3 = new JButton();
		b3.setText("3");
		b3.setBounds(200, 220, 80, 40);
		b3.addActionListener(this);
		b3.setVisible(true);
		calcLabel.add(b3);

		JButton bSub = new JButton();
		bSub.setText("-");
		bSub.setActionCommand("substract");
		bSub.setBounds(290, 220, 80, 40);
		bSub.addActionListener(this);
		bSub.setVisible(true);
		calcLabel.add(bSub);

		// fourth row
		JButton b0 = new JButton();
		b0.setText("0");
		b0.setBounds(20, 270, 80, 40);
		b0.addActionListener(this);
		b0.setVisible(true);
		calcLabel.add(b0);

		JButton bDot = new JButton();
		bDot.setText(".");
		bDot.setBounds(110, 270, 80, 40);
		bDot.addActionListener(this);
		bDot.setVisible(true);
		calcLabel.add(bDot);

		JButton bEqu = new JButton();
		bEqu.setText("=");
		bEqu.setActionCommand("equal");
		bEqu.setBounds(200, 270, 80, 40);
		bEqu.addActionListener(this);
		bEqu.setVisible(true);
		calcLabel.add(bEqu);

		JButton bPlus = new JButton();
		bPlus.setText("+");
		bPlus.setActionCommand("add");
		bPlus.setBounds(290, 270, 80, 40);
		bPlus.addActionListener(this);
		bPlus.setVisible(true);
		calcLabel.add(bPlus);

		// fifth row
		JButton bOpenBrac = new JButton();
		bOpenBrac.setText("(");
		bOpenBrac.setActionCommand("openBracket");
		bOpenBrac.setBounds(20, 320, 80, 40);
		bOpenBrac.addActionListener(this);
		bOpenBrac.setVisible(true);
		calcLabel.add(bOpenBrac);

		JButton bCloseBrac = new JButton();
		bCloseBrac.setText(")");
		bCloseBrac.setActionCommand("closeBracket");
		bCloseBrac.setBounds(110, 320, 80, 40);
		bCloseBrac.addActionListener(this);
		bCloseBrac.setVisible(true);
		calcLabel.add(bCloseBrac);

		JButton bMod = new JButton();
		bMod.setText("%");
		bMod.setActionCommand("modulo");
		bMod.setBounds(200, 320, 80, 40);
		bMod.addActionListener(this);
		bMod.setVisible(true);
		calcLabel.add(bMod);

		JButton bClear = new JButton();
		bClear.setText("CE");
		bClear.setActionCommand("clear");
		bClear.setBounds(290, 320, 80, 40);
		bClear.addActionListener(this);
		bClear.setVisible(true);
		calcLabel.add(bClear);

		// call pack() before setLocationRelativeTo ()
		// pack() determines the size of the window for centering the window
		calcFrame.pack();
		calcFrame.setLocationRelativeTo(null);
		// Display the window
		calcFrame.setVisible(true);
		calcFrame.setResizable(false);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String input = e.getActionCommand();

		switch (input) {
			case "add":
				input = "+";
				operator = '+';
				break;
			case "substract":
				input = "-";
				operator = '-';
				break;
			case "multiply":
				input = "x";
				operator = 'x';
				break;
			case "divide":
				input = "÷";
				operator = '÷';
				break;
			case "modulo":
				input = "%";
				operator = '%';
				break;
			case "equal":
				// clear screen and show result
				inputDisplayBuilder.delete(0, inputDisplayBuilder.length());

				if (operator == '+') {
					result = Double.parseDouble(inputNum1.toString()) + Double.parseDouble(inputNum2.toString());
				}
				if (operator == '-') {
					result = Double.parseDouble(inputNum1.toString()) - Double.parseDouble(inputNum2.toString());
				}
				if (operator == 'x') {
					System.out.println("multiply");
					result = Double.parseDouble(inputNum1.toString()) * Double.parseDouble(inputNum2.toString());
				}
				if (operator == '÷') {
					result = Double.parseDouble(inputNum1.toString()) / Double.parseDouble(inputNum2.toString());
				}
				if (operator == '%') {
					result = Double.parseDouble(inputNum1.toString()) % Double.parseDouble(inputNum2.toString());
				}

				calcScreen.setText(String.valueOf(result));
				break;
			case "clear":
				inputDisplayBuilder.delete(0, inputDisplayBuilder.length());
				result = 0;
				calcScreen.setText(String.valueOf(result));
				inputNum1.delete(0, inputNum1.length());
				inputNum2.delete(0, inputNum2.length());
				operator = 0;

				break;
			default:
				if (operator == 0) {
					inputNum1.append(input);
				} else {
					inputNum2.append(input);
				}
				break;
		}

		if (!input.equals("equal") && !input.equals("clear")) {
			inputDisplayBuilder.append(input);
			calcScreen.setText(inputDisplayBuilder.toString());
		}
	}

	public static void main(String args[]) {
		Calculator calc = new Calculator();
		calc.show();
	}

}