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


 

50 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
  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

Leave a Reply

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