Hello guys today I am going to learn you how to create a simple Java calculator by using JFrame.This is the most simple calculator you have ever seen so it’s an example for beginners.

Let’s explain it.

First you need 2 text fields for entering 2 numbers.You also need 4 buttons. A plus button, minus button, division button and multiplication button. With these buttons you can operate with your numbers.And finally, a button to clear the fields and label to display your result.

package application;

import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class calculator extends JFrame implements ActionListener {
  
  private JButton minus,plus,divide,multiplication,clear;
  private TextField text1,text2;
  private JLabel label;
  private double res = 0;
  
  public calculator()
  {
    setLayout(new FlowLayout());
    
    //we set the size of the frame and make it visible
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    
    //initialize the buttons and add them to the frame
    minus = new JButton("-");
    minus.addActionListener(this);
    add(minus);
    
    plus = new JButton("+");
    plus.addActionListener(this);
    add(plus);
    
    divide = new JButton("/");
    divide.addActionListener(this);
    add(divide);
    
    multiplication = new JButton("*");
    multiplication.addActionListener(this);
    add(multiplication);
    
    clear = new JButton("clear");
    clear.addActionListener(this);
    add(clear);
    
    text1 = new TextField(" ",10);
    add(text1);
    
    text2 = new TextField(" ",10);
    add(text2);
    
    label = new JLabel("");
    add(label);
    
  }

  
  public static void main(String[] args) 
  {
    new calculator();

  }

  
  public void actionPerformed(ActionEvent e) 
  {
     //we parse the text from textfield to double
    double nr1 = Double.parseDouble(text1.getText().toString());
    double nr2 = Double.parseDouble(text2.getText().toString());
    
    //here we check what happens when each button is clicked
    //and display the result
    if(e.getSource() == minus)
    {
      res = nr1 - nr2;
      label.setText(res + " ");
    }
    
    else if(e.getSource() == plus)
    {
      res = nr1 + nr2;
      label.setText(res + " ");
    }
    
    else if (e.getSource() == multiplication)
    {
      res = nr1 * nr2;
      label.setText(res + " ");
    }
    
    else if(e.getSource() == divide)
    {
      res = nr1 / nr2;
      label.setText(res + " ");
    }
    
    //here we empty the fields
    else if(e.getSource() == clear)
    {
      text1.setText(" ");
      text2.setText(" ");
      label.setText("");
    }
  }
  
}

 

The compiled Program

calculator



17 COMMENTS

  1. What’s up,I read your blog named “How to create a calculator in Java (for beginners) – Bit Life” on a regular basis.Your writing style is awesome, keep up the good work! And you can look our website about love spells for free.

  2. Howdy, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you stop it, any plugin or anything you can suggest? I get so much lately it’s driving me crazy so any assistance is very much appreciated.

  3. Hello! I know this is somewhat off topic but I was wondering which blog platform are
    you using for this website? I’m getting sick and tired
    of WordPress because I’ve had problems with hackers and I’m looking at alternatives for
    another platform. I would be awesome if you could point
    me in the direction of a good platform.

LEAVE A REPLY

Please enter your comment!
Please enter your name here