Home Java Example Java Swing Writing Calculator Program in Swing


Share on Google+Share on Google+

Writing Calculator Program in Swing

In this tutorial we are providing you an example which illustrates you how to a create calculator in Swing with the source code and screen shot.

Writing Calculator Program in Swing


In this tutorial we are providing you an example which illustrates you how to a create calculator in Swing with the source code and screen shot.

For developing a small calculator program in swing we need two different classes 

1) SwingCalculator.java
2) Calculator.java

The SwingCalculator.java calls the Calculator.java class by JFrame frame = new Calculator(). All the methods and actions are to be performed in Calculator.java class.

Calculator Code in Java Swing

Please save the code as SwingCalculator.java

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

class SwingCalculator {
 public static void main(String[] args) {
  JFrame frame = new Calculator();

Here is the code of Calculator.java

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

  class Calculator extends JFrame {
  private final Font BIGGER_FONT = new Font("monspaced",
  private JTextField textfield; 
  private boolean number = true;  
  private String  equalOp  = "=";  
  private CalculatorOp op = new CalculatorOp(); 

  public Calculator() {
  textfield = new JTextField("0"12);
  ActionListener numberListener = new NumberListener();
  String buttonOrder = "1234567890 ";
  JPanel buttonPanel = new JPanel();
  buttonPanel.setLayout(new GridLayout(4444));
  for (int i = 0; i < buttonOrder.length(); i++) {
  String key = buttonOrder.substring(i, i+1);
  if (key.equals(" ")) {
  buttonPanel.add(new JLabel(""));
  else {
  JButton button = new JButton(key);
  ActionListener operatorListener = new OperatorListener();
  JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(4444));
  String[] opOrder = {"+""-""*""/","=","C"};
 for (int i = 0; i < opOrder.length; i++) {
  JButton button = new JButton(opOrder[i]);
 JPanel pan = new JPanel();
  pan.setLayout(new BorderLayout(44));
  pan.add(textfield, BorderLayout.NORTH );
  pan.add(buttonPanel , BorderLayout.CENTER);
  pan.add(panel , BorderLayout.EAST  );
  private void action() {
  number = true
  equalOp  = "=";
  class OperatorListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
  if (number) {
  else {
  number = true
  String displayText = textfield.getText();
  if (equalOp.equals("=")) {
  else if (equalOp.equals("+")) {
  else if (equalOp.equals("-")) {
  else if (equalOp.equals("*")) {
  else if (equalOp.equals("/")) {
 textfield.setText("" + op.getTotalString());
 equalOp = e.getActionCommand();
  class NumberListener implements ActionListener {
  public void actionPerformed(ActionEvent event) {
  String digit = event.getActionCommand(); 
  if (number) {
  number = false;
  else {
 textfield.setText(textfield.getText() + digit);
  public class CalculatorOp {
private int total; 
public CalculatorOp() {
  total = 0;
 public String getTotalString() {
  return ""+total;
 public void setTotal(String n) {
  total = convertToNumber(n);
 public void add(String n) {
  total += convertToNumber(n);
 public void subtract(String n) {
  total -= convertToNumber(n);
 public void multiply(String n) {
  total *= convertToNumber(n);
   public void divide(String n) {
  total /= convertToNumber(n);
 private int convertToNumber(String n) {
  return Integer.parseInt(n);

The constructor new CalculatorOp() calls the CalculatorOp class. The Swing component JTextField is used to create textbox on which calculation is to be performed. JPanel arranges the numeric buttons in a panel. JButton is used to perform an action. OperatorListener class is called to perform action on operators, i.e, '+,-,*,/,='. The class NumberListener is called for numbers 0 to 9.

Output will be displayed as:

Download Source Code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 12, 2008

Related Tutorials

Ask Questions?    Discuss: Writing Calculator Program in Swing   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 26, 2011

My friend writing a calulator program but unable to write exact code. code present in this website will more usefull to me and aswell as my friend. Thank you.
June 27, 2013
from iran

thank you
poltu haldar
August 31, 2011

the calculator is so nice
Muhammad Burhan
September 1, 2011
Suggestion ...

Asslam O Alaikum Sir... I jst check ur code but not execute it ... May b it will work well... I have a humble suggestion about this code.. which i s... plz commented every line action with it which will help new comers and learners.. thanx... Best Regargds.. Muhammad Burhan Mughal Punjba University Pakistan ....
November 10, 2011

thnx for providing coding,
Trust Birungi
November 24, 2011
Extending functionality of the calculator

I have tried to add other operations such as square, cube, modulus, cube root and others but i have failed. Can you show me how i might extend the operations of the calculator?
December 14, 2011
algorithm of above program

im new in java i got an assignment to create a calculator using swing .plese help
December 15, 2011
Distributed Java

satyendra kumar
December 17, 2011

in your website the calculator program written without main method . is it possible ?
January 15, 2013

good program continue
February 3, 2012

dear sir i am getting error in calculator program"Nosuchmethoderror" during runtime
Reza M
February 7, 2012

i guess in constructor you must call super()... public Calculator() { super();
Reza M
February 7, 2012

i guess in constructor you must call super()... public Calculator() { super();
Vasu Birla
February 24, 2012

sir there is an eight errors in above java program.
February 28, 2012

how do u get the calculator to work?
March 5, 2012

ei thx for the codes it really helps me in my subject....more power sir..
Suman Shandilya
March 15, 2012
Learn Core Java

Sir, I want learn java please help me. thanks..
Dharam dev Das
March 22, 2012

i am a student
March 27, 2012
premium calculator

Plz anyone can mail me code of premium calculator in java(jsp)
Zaheer Abbas
June 12, 2012

main method is not in the program.
June 19, 2012
programming l

i like programming
July 23, 2012

Exception error is generated
August 9, 2012
java swings

i love this website...... i am happy to seen this website.. becoz i need to learn more in java through this valuable site.. thank you!!!!!!!!
November 10, 2012
Great code

Code is very easy to implement. It is also short. Thanks.
erika ortega
November 25, 2012

i want a basic codes ... i want to lern more
adeela azam
May 28, 2014

plz explain each line of code bcz i don't know some classes.plz help me..
March 27, 2014
Codes with explanations

Can u tell me the explanations with codes so that i'll come to know the whole code what is used to create a calculator ??
March 27, 2014
functions of code explanation

Can you explain each functions of code of swing calculator ??
praveen kumar
September 14, 2012

send a simple and easy program for scientific calculator using swing in java coding