# Java applet program for calculator

- Link: https://technotaught.com/java-applet-program-for-calculator/
- Published: 2019-07-24T21:52:07+05:30
- Author: Ashishkumar Vishwakarma

A Java applet program for the calculator. Use a grid layout to arrange buttons for
the digits and for the Addition, Subtraction, Multiplication, Division operations.
Add a text field to display the First and Second Numbers and results.

 Also, you can use the Java [compiler](https://www.onlinegdb.com/online_java_compiler)
to compile a program.

#### Implementation.

#### Output:

#### Implementation.

    ```javascript
    import java.awt.*;
    import java.awt.event.*;

    class calc implements ActionListener
    {
        //Declaring Objects
        Frame f=new Frame();
        Label l1=new Label("First Number");
        Label l2=new Label("Second Number");
        Label l3=new Label("Result");

        TextField t1=new TextField();
        TextField t2=new TextField();
        TextField t3=new TextField();

        Button b1=new Button("Add");
        Button b2=new Button("Sub");
        Button b3=new Button("Mul");
        Button b4=new Button("Div");
        Button b5=new Button("Cancel");

        calc()
        {
            //Giving Coordinates
            l1.setBounds(100,100,150,30);
            l2.setBounds(100,140,150,30);
            l3.setBounds(100,180,150,30);

            t1.setBounds(300,100,150,30);
            t2.setBounds(300,140,150,30);
            t3.setBounds(300,180,150,30);

            b1.setBounds(50,350,60,30);
            b2.setBounds(110,350,60,30);
            b3.setBounds(170,350,60,30);
            b4.setBounds(230,350,60,30);
            b5.setBounds(290,350,60,30);

            //Adding components to the frame
            f.add(l1);
            f.add(l2);
            f.add(l3);

            f.add(t1);
            f.add(t2);
            f.add(t3);

            f.add(b1);
            f.add(b2);
            f.add(b3);
            f.add(b4);
            f.add(b5);

            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);
            b5.addActionListener(this);

            f.setLayout(null);
            f.setVisible(true);
            f.setSize(500,450);
        }

        public void actionPerformed(ActionEvent e)
        {
            int n1=Integer.parseInt(t1.getText());
            int n2=Integer.parseInt(t2.getText());

            if(e.getSource()==b1)
            {
                t3.setText(String.valueOf(n1+n2));
            }

            if(e.getSource()==b2)
            {
                t3.setText(String.valueOf(n1-n2));
            }

            if(e.getSource()==b3)
            {
                t3.setText(String.valueOf(n1*n2));
            }

            if(e.getSource()==b4)
            {
                t3.setText(String.valueOf(n1/n2));
            }

            if(e.getSource()==b5)
            {
                System.exit(0);
            }
        }

        public static void main(String...s)
        {
            new calc();
        }
    }
    ```

#### Output:

    ```wp-block-preformatted
     C:\A> javac calc.java
     C:\A> java calc
    ```

![Java applet program for calculator](https://technotaught.com/wp-content/uploads/
2019/07/image-47.png)

calculator

Also View:[ – Write a program to create a registration form using AWT](https://technotaught.com/registration-form/).

Also View- [Write a java program to create a window using swing.](https://technotaught.com/22-write-a-java-program-to-create-a-window-using-swing/)

### Share this:

 * [  X ](https://technotaught.com/java-applet-program-for-calculator/?share=twitter)
 * [  Facebook ](https://technotaught.com/java-applet-program-for-calculator/?share=facebook)
 * [More](https://technotaught.com/java-applet-program-for-calculator/#)

 * [  WhatsApp ](https://technotaught.com/java-applet-program-for-calculator/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/java-applet-program-for-calculator/?share=linkedin)
 * [  Email ](https://technotaught.com/java-applet-program-for-calculator/?subject=%5BShared%20Post%5D%20Java%20applet%20program%20for%20calculator&body=https%3A%2F%2Ftechnotaught.com%2Fjava-applet-program-for-calculator%2F&share=email)
 * [  Reddit ](https://technotaught.com/java-applet-program-for-calculator/?share=reddit)
 * [  Tumblr ](https://technotaught.com/java-applet-program-for-calculator/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/java-applet-program-for-calculator/?share=pinterest)
 * [  Telegram ](https://technotaught.com/java-applet-program-for-calculator/?share=telegram)
 * [  Print ](https://technotaught.com/java-applet-program-for-calculator/#print?share=print)

### _Related_
