Java swing program for the simple calculator


In this program, You will learn how to create a simple calculator using swing in java.


20 = 10 + 10

10 = 20 - 10

Example: How to create a simple calculator using swing in java.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Test extends JFrame implements ActionListener {

    JButton jb1, jb2, jb3, jb4;
    JTextField jt1, jt2;
    JLabel lbl;

    Test() {

        jt1 = new JTextField();
        jt1.setBounds(90, 50, 150, 30);
        add(jt1);

        jt2 = new JTextField();
        jt2.setBounds(90, 80, 150, 30);
        add(jt2);

        lbl = new JLabel("Result :");
        lbl.setBounds(90, 140, 150, 30);
        add(lbl);

        jb1 = new JButton("+");
        jb1.setBounds(50, 200, 100, 30);
        add(jb1);

        jb2 = new JButton("-");
        jb2.setBounds(150, 200, 100, 30);
        add(jb2);

        jb3 = new JButton("*");
        jb3.setBounds(250, 200, 100, 30);
        add(jb3);

        jb4 = new JButton("/");
        jb4.setBounds(350, 200, 100, 30);
        add(jb4);

        jb1.addActionListener(this);
        jb2.addActionListener(this);
        jb3.addActionListener(this);
        jb4.addActionListener(this);

        setLayout(null);
        setSize(600, 500);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {

        int a = Integer.parseInt(jt1.getText());
        int b = Integer.parseInt(jt2.getText());
        int c = 0;

        if (e.getSource().equals(jb1)) {
            c = a + b;
            lbl.setText(String.valueOf(c));
        } else if (e.getSource().equals(jb2)) {
            c = a - b;
            lbl.setText(String.valueOf(c));
        } else if (e.getSource().equals(jb3)) {
            c = a * b;
            lbl.setText(String.valueOf(c));
        } else if (e.getSource().equals(jb4)) {
            c = a / b;
            lbl.setText(String.valueOf(c));
        }
    }

    public static void main(String args[]) {
        Test t = new Test();
    }
}

Output:

java swing simple calculator