How to Make a Calculator in Java Using Swing

This tutorial is about how to make a calculator in Java. Below I have shared the simple calculator program in java using swing. It is a simple calculator in Java which can perform basic arithmetic operations like addition, subtraction, multiplication and division of two numbers. If you are getting any difficulty to understand or run this program then comment below, I will try to solve problem.
Program to Create Calculator Using Java Swing

 

Program to Make a Calculator in Java Using Swing


 

60 thoughts on “How to Make a Calculator in Java Using Swing

  1. Nahida

    Everything worked nicely but when I click on any of the numbers, nothing appears in the text field. And if I click on the operators there are lots of errors. Please help.

    Reply
    1. Admin Post author

      Yes there was some problem in it, now I fixed. Please run it and let me know it is working or not.

      Reply
  2. Maaz Ali

    Very Nice and simple code…..
    the first program i run and it give zero errors… rockzzzzzzzzzzzzzzzzz

    Reply
  3. vishal

    addition of more than 3 nos not possible.it is considering last 2 nos only for all arthematic operations

    Reply
  4. khan

    Error: could not find or load main class cal
    it give only this option nothing else

    Reply
  5. ANON

    Thanks alot, this helped me and taught me exactly the things i was looking to learn

    Reply
    1. wasil

      you have to add every component separately.This is the only way. Or you can use IDE ,you wont have to write anything accept the code logic there.

      Reply
  6. sanchita khadka

    simple and understanding code. helped me to understand how it worked thanks a loads. looking forward for other codes as well.

    Reply
  7. abdizhakur

    thanks you
    i went to help me the button of point
    and other additionalbuttons

    Reply
  8. Harjot Singh

    run time error will come “could not find or load main class calc”

    Reply
    1. Admin Post author

      Class name is case sensitive while running. Use Calc instead of calc.

      Reply
  9. Aayana

    heya
    this program is running bt this is showing null screen

    Reply
  10. Aditya

    If I hit any operator without entering any values, I want the program to show a message “Enter values”. How can I achieve that?

    Reply
  11. Ak

    By clicking on any of operators,number entered gets deleted instead of respective operations to be performed

    Reply
  12. Tisha P P

    Very useful code…Thanks a lot

    public void actionPerformed(ActionEvent e) {
    String n=e.getActionCommand();
    switch (n) {

    case “.” : if(txt.getText().contains(“.”)) {}
    else {
    txt.setText(txt.getText().concat(“.”));
    }
    break;

    }

    Include this piece of code while checking for decimal point. It is to check if a decimal already exists or not, if does not exists, concatenates “.” with the number.
    Try this…!!

    Reply
  13. uv

    //equals to button doesnot work… somebody help..
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class calculator implements ActionListener {
    static double a=0,b=0;
    static int operator;
    double result=0;
    JFrame f;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,badd,bsub,bmul,bdiv,bclr,beql;
    JPanel p;
    JTextField t;
    calculator(){
    f=new JFrame(“Calculator”);
    f.setSize(500,500);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setLayout(new GridLayout(2,1));

    p=new JPanel();
    b1=new JButton(“1”);
    b2=new JButton(“2”);
    b3=new JButton(“3”);
    b4=new JButton(“4”);
    b5=new JButton(“5”);
    b6=new JButton(“6”);
    b7=new JButton(“7”);
    b8=new JButton(“8”);
    b9=new JButton(“9”);
    b10=new JButton(“0”);
    badd=new JButton(“+”);
    bsub=new JButton(“-“);
    bmul=new JButton(“*”);
    bdiv=new JButton(“/”);
    bclr=new JButton(“Clear”);
    beql=new JButton(“=”);
    p.setLayout(new GridLayout(4,4));

    t=new JTextField(10);
    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);
    p.add(b5);
    p.add(b6);
    p.add(b7);
    p.add(b8);
    p.add(b9);
    p.add(b10);
    p.add(badd);
    p.add(bsub);
    p.add(bmul);
    p.add(bdiv);
    p.add(bclr);
    p.add(beql);
    f.add(t);
    f.add(p);
    p.setVisible(true);
    f.setVisible(true);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);
    b10.addActionListener(this);
    badd.addActionListener(this);
    bsub.addActionListener(this);
    bmul.addActionListener(this);
    bdiv.addActionListener(this);
    bclr.addActionListener(this);
    beql.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==b1){
    t.setText(t.getText().concat(“1”)) ;
    }
    if(e.getSource()==b2){
    t.setText(t.getText().concat(“2”));
    }
    if(e.getSource()==b3){
    t.setText(t.getText().concat(“3”));
    }
    if(e.getSource()==b4){
    t.setText(t.getText().concat(“4”));
    }
    if(e.getSource()==b5){
    t.setText(t.getText().concat(“5”));
    }
    if(e.getSource()==b6){
    t.setText(t.getText().concat(“6”));
    }
    if(e.getSource()==b7){
    t.setText(t.getText().concat(“7”));
    }
    if(e.getSource()==b8){
    t.setText(t.getText().concat(“8”));
    }
    if(e.getSource()==b9){
    t.setText(t.getText().concat(“9”));
    }
    if(e.getSource()==b10){
    t.setText(t.getText().concat(“0”));
    }
    if(e.getSource()==badd){
    a=Double.parseDouble(t.getText());
    operator=1;
    t.setText(t.getText().concat(“+”));
    }
    if(e.getSource()==bsub){
    a=Double.parseDouble(t.getText());
    operator=2;
    t.setText(t.getText().concat(“-“));
    }
    if(e.getSource()==bmul){
    a=Double.parseDouble(t.getText());
    operator=3;
    t.setText(t.getText().concat(“*”));
    }
    if(e.getSource()==bdiv){
    a=Double.parseDouble(t.getText());
    operator=4;
    t.setText(t.getText().concat(“/”));
    }
    if(e.getSource()==bclr){
    t.setText(“”);

    }
    if(e.getSource()==beql){
    b=Double.parseDouble(t.getText());
    switch(operator){
    case 1:
    result=a+b;
    break;
    case 2:
    result=a-b;
    break;
    case 3:
    result=a*b;
    break;
    case 4:
    result=a/b;
    break;
    default:
    result=0;
    }
    t.setText(“”+result);
    }

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

    Reply
  14. Tisha

    Better use switch() instead of if() in method actionPerformed(). So there is no need of =

    Reply
  15. Apoorva

    //answer is not coming…please help

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    class calc extends JFrame implements ActionListener
    {
    JFrame f; TextArea t; JPanel jp;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;
    calc()
    {
    f= new JFrame(“Panel “);
    f.getContentPane().setBackground(Color.cyan);
    t=new TextArea();
    t.setBounds(40,10,200,200);
    GridLayout gl=new GridLayout(5,4);
    jp=new JPanel(gl);
    jp.setBounds(40,80,200,200);
    jp.setBackground(Color.gray);

    b1=new JButton(“C”); b1.setBackground(Color.gray);
    b2=new JButton(“x2”); b2.setBackground(Color.gray);
    b3=new JButton(“x3”); b3.setBackground(Color.gray);
    b4=new JButton(“0”); b4.setBackground(Color.gray);
    b5=new JButton(“1”); b5.setBackground(Color.gray);
    b6=new JButton(“2”); b6.setBackground(Color.gray);
    b7=new JButton(“3”); b7.setBackground(Color.gray);
    b8=new JButton(“%”); b8.setBackground(Color.gray);
    b9=new JButton(“4”); b9.setBackground(Color.gray);
    b10=new JButton(“5”); b10.setBackground(Color.gray);
    b11=new JButton(“6”); b11.setBackground(Color.gray);
    b12=new JButton(“.”); b12.setBackground(Color.gray);
    b13=new JButton(“7”); b13.setBackground(Color.gray);
    b14=new JButton(“8”); b14.setBackground(Color.gray);
    b15=new JButton(“9”); b15.setBackground(Color.gray);
    b16=new JButton(“=”); b16.setBackground(Color.gray);
    b17=new JButton(“+”); b17.setBackground(Color.gray);
    b18=new JButton(“-“); b18.setBackground(Color.gray);
    b19=new JButton(“*”); b19.setBackground(Color.gray);
    b20=new JButton(“/”); b20.setBackground(Color.gray);

    jp.add(b1); jp.add(b2); jp.add(b3); jp.add(b4); jp.add(b5); jp.add(b6);
    jp.add(b7); jp.add(b8); jp.add(b9); jp.add(b10); jp.add(b11); jp.add(b12);
    jp.add(b13); jp.add(b14); jp.add(b15); jp.add(b16); jp.add(b17);
    jp.add(b18); jp.add(b19); jp.add(b20);

    b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this);
    b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this);
    b9.addActionListener(this); b10.addActionListener(this); b11.addActionListener(this); b12.addActionListener(this);
    b13.addActionListener(this); b14.addActionListener(this); b15.addActionListener(this); b16.addActionListener(this);
    b17.addActionListener(this); b18.addActionListener(this); b19.addActionListener(this); b20.addActionListener(this);

    f.add(jp);
    f.add(t);
    f.setSize(400,400);
    f.setLayout(null);
    f.setVisible(true);
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e)
    {
    int ch=0; int a=0;
    if(e.getSource()==b1)
    {
    t.setText(“0”);
    }
    if(e.getSource()==b2)
    {
    String s=t.getText();
    int x=Integer.parseInt(s);
    x=x*x;
    String c=String.valueOf(x);
    t.setText(c);
    }
    if(e.getSource()==b3)
    {
    String s=t.getText();
    int x=Integer.parseInt(s);
    x=x*x*x;
    String c=String.valueOf(x);
    t.setText(c);
    }
    if(e.getSource()==b4)
    {
    String s=t.getText().concat(“0”);
    t.setText(s);
    }
    if(e.getSource()==b5)
    {
    String s=t.getText().concat(“1”);
    t.setText(s);
    }
    if(e.getSource()==b6)
    {
    String s=t.getText().concat(“2”);
    t.setText(s);
    }
    if(e.getSource()==b7)
    {
    String s=t.getText().concat(“3”);
    t.setText(s);
    }
    if(e.getSource()==b8)
    {
    String s=t.getText().concat(“%”);
    ch=1;
    t.setText(” “);
    }
    if(e.getSource()==b9)
    {
    String s=t.getText().concat(“4”);
    t.setText(s);
    }
    if(e.getSource()==b10)
    {
    String s=t.getText().concat(“5”);
    t.setText(s);
    }
    if(e.getSource()==b11)
    {
    String s=t.getText().concat(“6”);
    t.setText(s);
    }
    if(e.getSource()==b12)
    {
    String s=t.getText().concat(“.”);
    t.setText(s);
    }
    if(e.getSource()==b13)
    {
    String s=t.getText().concat(“7”);
    t.setText(s);
    }
    if(e.getSource()==b14)
    {
    String s=t.getText().concat(“8”);
    t.setText(s);
    }
    if(e.getSource()==b15)
    {
    String s=t.getText().concat(“9”);
    t.setText(s);
    }
    if(e.getSource()==b17)
    {
    //String s=t.getText().concat(“+”);
    String s=t.getText();
    a=Integer.parseInt(s);
    t.setText(” “);
    ch=2;
    // t.setText(s);
    }
    if(e.getSource()==b18)
    {
    //String s=t.getText().concat(“-“);
    a=Integer.parseInt(t.getText());
    t.setText(” “);
    ch=3;
    // t.setText(s);
    }
    if(e.getSource()==b19)
    {
    //String s=t.getText().concat(“*”);
    a=Integer.parseInt(t.getText());
    t.setText(” “);
    ch=4;
    //t.setText(s);
    }
    if(e.getSource()==b20)
    {
    //String s=t.getText().concat(“/”);
    a=Integer.parseInt(t.getText());
    t.setText(” “);
    ch=5;
    //t.setText(s);
    }

    if(e.getSource()==b16)
    {
    int b=Integer.parseInt(t.getText());
    int res;
    switch(ch)
    {
    case 1:
    res=a%b;
    t.setText(String.valueOf(res));
    break;
    case 2:
    res=a+b;
    t.setText(String.valueOf(res));
    break;
    case 3:
    res=a-b;
    t.setText(String.valueOf(res));
    break;
    case 4:
    res=a*b;
    t.setText(String.valueOf(res));
    break;
    case 5:
    res=a/b;
    t.setText(String.valueOf(res));
    break;
    default:
    t.setText(“0”);

    }
    }
    }

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

    Reply
    1. Tisha P P

      I think there is no need of JPanel. You are using a JPanel and adds all the components to the panel but the JPanel is not added to the JFrame.

      Reply
  16. A.MADHURI

    Instead of adding each button using setBounds() u can simply create a gridlayout and manage the buttons more easily…just a suugestion to make the program shoter and simpler

    Reply
  17. MADHURI A

    I saw your LinkedIn profile got to know u r frm K.V bro.. evn Im frm K.V 🙂

    Reply
    1. Admin Post author

      Yeh glad to know that, thanks for visiting my blog. 🙂

      Reply
  18. abi

    im getting ans for addition operator in text filed calculation part but multiplication and divsion part is wrong help me pls….

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Container;
    class Calcu extends JFrame implements ActionListener
    {
    Container container;
    JPanel p;
    JTextField t;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bsub,bmul,bdiv,beql,bclr,bdot,bdel;
    int op;
    int a,b,result;
    Calcu()
    {

    container=getContentPane();
    ButtonGroup g=new ButtonGroup();
    p=new JPanel();
    setVisible(true);
    setSize(500,500);
    p.setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle(“CALCULATOR”);
    t=new JTextField();
    t.setBounds(30,40,280,30);
    b1=new JButton(“1”);
    b1.setBounds(40,240,50,40);
    b1.addActionListener(this);
    b2=new JButton(“2”);
    b2.setBounds(110,240,50,40);
    b2.addActionListener(this);
    b3=new JButton(“3”);
    b3.setBounds(180,240,50,40);
    b3.addActionListener(this);
    b4=new JButton(“4”);
    b4.setBounds(40,170,50,40);
    b4.addActionListener(this);
    b5=new JButton(“5”);
    b5.setBounds(110,170,50,40);
    b5.addActionListener(this);
    b6=new JButton(“6”);
    b6.setBounds(180,170,50,40);
    b6.addActionListener(this);
    b7=new JButton(“7”);
    b7.setBounds(40,100,50,40);
    b7.addActionListener(this);
    b8=new JButton(“8”);
    b8.setBounds(110,100,50,40);
    b8.addActionListener(this);
    b9=new JButton(“9”);
    b9.setBounds(180,100,50,40);
    b9.addActionListener(this);
    b0=new JButton(“0”);
    b0.setBounds(110,310,50,40);
    b0.addActionListener(this);
    badd=new JButton(“+”);
    badd.setBounds(250,310,50,40);
    badd.addActionListener(this);
    bsub=new JButton(“-“);
    bsub.setBounds(250,240,50,40);
    bsub.addActionListener(this);
    bdiv=new JButton(“/”);
    bdiv.setBounds(250,100,50,40);
    bdiv.addActionListener(this);
    bmul=new JButton(“*”);
    bmul.setBounds(250,170,50,40);
    bmul.addActionListener(this);
    bdot=new JButton(“.”);
    bdot.setBounds(40,310,50,40);
    bdot.addActionListener(this);
    beql=new JButton(“=”);
    beql.setBounds(180,310,50,40);
    beql.addActionListener(this);
    bclr=new JButton(“clear”);
    bclr.setBounds(180,380,100,40);
    bclr.addActionListener(this);
    bdel=new JButton(“delete”);
    bdel.setBounds(60,380,100,40);
    bdel.addActionListener(this);

    p.add(t);
    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);
    p.add(b5);
    p.add(b6);
    p.add(b7);
    p.add(b8);
    p.add(b9);
    p.add(b0);
    p.add(badd);
    p.add(bsub);
    p.add(bdiv);
    p.add(bmul);
    p.add(beql);
    p.add(bclr);
    p.add(bdot);
    p.add(bdel);
    container.add(p);
    }
    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource()==b1)
    t.setText(t.getText().concat(“1”));
    if(e.getSource()==b2)
    t.setText(t.getText().concat(“2”));
    if(e.getSource()==b3)
    t.setText(t.getText().concat(“3”));
    if(e.getSource()==b4)
    t.setText(t.getText().concat(“4”));
    if(e.getSource()==b5)
    t.setText(t.getText().concat(“5”));
    if(e.getSource()==b6)
    t.setText(t.getText().concat(“6”));
    if(e.getSource()==b7)
    t.setText(t.getText().concat(“7”));
    if(e.getSource()==b8)
    t.setText(t.getText().concat(“8”));
    if(e.getSource()==b9)
    t.setText(t.getText().concat(“9”));
    if(e.getSource()==b0)
    t.setText(t.getText().concat(“0”));
    if(e.getSource()==badd)
    t.setText(t.getText().concat(“+”));
    if(e.getSource()==bsub)
    t.setText(t.getText().concat(“-“));
    if(e.getSource()==bdiv)
    t.setText(t.getText().concat(“/”));
    if(e.getSource()==bmul)
    t.setText(t.getText().concat(“*”));
    if(e.getSource()==beql)
    t.setText(t.getText().concat(“=”));
    if(e.getSource()==bdot)
    t.setText(t.getText().concat(“.”));

    if(e.getSource()==bclr){

    t.setText(” “);
    t.requestFocus();
    }

    if(e.getSource()==badd)
    {
    a=Integer.parseInt(t.getText());
    op=1;

    }
    if(e.getSource()==bsub)
    {
    a=Integer.parseInt(t.getText());
    op=2;

    }
    if(e.getSource()==bdiv)
    {
    a=Integer.parseInt(t.getText());
    op=3;

    }
    if(e.getSource()==bmul)
    {
    a=Integer.parseInt(t.getText());
    op=4;

    }
    if(e.getSource()==beql)
    {
    b=Integer.parseInt(t.getText());
    switch(op)
    {
    case 1:result=a+b;
    break;
    case 2:result=a-b;
    break;
    case 3:result=a/b;
    break;
    case 4:result=a*b;
    break;

    }
    t.setText(“”+result);
    }
    if (e.getSource()==bclr)
    t.setText(“”);

    if(e.getSource()==bdel)
    {
    String s=t.getText();
    t.setText(“”);
    for(int i=0;i<s.length()-1;i++)
    t.setText(t.getText()+s.charAt(i));
    }
    }

    public static void main(String arg[])
    {

    new Calcu().setVisible(true);

    }
    }

    Reply
  19. tadele

    what a nice simple and robust program! if you add program to perform square root,it shall be better.any way thank you

    Reply
  20. Matthew

    I don’t understand the purpose of the static double variables “a” and “b” at the beginning of the code. What are they used for and why are they static? Being static wouldn’t any use of them give the default value 0? I’m just a beginner who was looking to challenge myself by making a calculator app so please be kind and explain in not too complex of a manner. Thank you!

    Reply
    1. Admin Post author

      In this program static doesn’t make any difference. Non-static a and b also work the same in this case. Maybe I have accidentally declared them static.

      Reply
  21. aliyi sufiyan

    i appriciate ur coding format . it is understandable

    Reply
  22. fayzan

    can you please put a code where you can add +/- function as well to the number. like subtracting 2 negative numbers or adding 2 negative numbers

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *