Calculator written in Java Programming Language (for beginners)

Calculator-Java

Hello guys today we’re going to teach 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.

Some introductions first:

JFrame is a class in java which has pre-written methods and constructors that allow you to build the design of your calculator application and many others. Think of JFrame as a big place where you can find elements like, panels, buttons, text areas, labels etc. You will understand more and feel very good after writing the code and seeing your calculator run perfectly.

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. Creating a calculator is a great way to start practicing so follow along.

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("");
    }
  }
  
}

 

All you have to do now is run and enjoy using it. If you come to discover some bugs don’t worry about it. Think of it as a way to motivate you to learn code everyday in order to make progress.

Voila! The compiled Program

Calculator-program

One example of what would an improvement be for this calculator is: since the label displays a double it always shows the result with a decimal value. In the future we will post another simple calculator that is build using JavaFX where the code is more complex and with no bugs present. JavaFX offers some very nice designs of building elements: textfields, panes, buttons, labels etc. You can customize every single one of them.

Stay tuned for more amazing content and make sure to read our posts to learn new stuff!

LEAVE A REPLY

Please enter your comment!
Please enter your name here