Java Program to Make a Simple Calculator Using AWT

Hello everyone, in this program I am going to share a code to make a simple calculator in java using awt. This calculator works on two integer numbers. As you enter two numbers and then click on desired button, the result is shown in Result text field. When you will click on Cancel button, the calculator will be closed. So just go through the code and try it. If you have any difficulty in understanding or using the code, then you ask by commenting below.

 

 

Java Program to Make a Simple Calculator Using AWT

 

Category: AWT

15 thoughts on “Java Program to Make a Simple Calculator Using AWT

  1. harry dufferin

    PLEASE WHAT IS AWT,and thanks for the code but enligthen me further..

    Reply
    1. Neeraj Mishra

      AWT is one of the way to do GUI programming in java. It is a package that contains all classes and interface that are needed to implement GUI. I hope you understand what i want to say.
      Thanks for your valuable comment.

      Reply
  2. muhammad nasir

    It gives this error message not compiling
    The method addActionListener(ActionListener) in the type Button is not applicable for the arguments (Calculator) Calculator.java
    Gives error at this part:
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);

    Reply
    1. nagaraju chandu

      It gives this error message not compiling
      The method addActionListener(ActionListener) in the type Button is not applicable for the arguments (Calculator) Calculator.java
      thi part error plz tell the resone,I am wait for result so do fast .
      Gives error at this part:
      b1.addActionListener(this);
      b2.addActionListener(this);
      b3.addActionListener(this);
      b4.addActionListener(this);
      b5.addActionListener(this);

      Reply ↓

      Reply
  3. Krishan Thakar

    How can I remove the cancel option to close and allow the red x to close

    Reply
    1. Supreet

      modify main as

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

      cal.addWindowListener(new WindowAdapter()
      {
      public void windowClosing(WindowEvent e)
      {
      System.exit(0);
      }
      });
      }

      and in imports add

      import java.awt.event.WindowListener;

      Reply
  4. zubi

    What is “This” , error occur at this part ,plz help me….!!!

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

    Reply
    1. bhargav

      no its right if you have cut pasted the code you should change that comment section

      Reply
  5. Shakti

    What is the significance of ActionEvent e ?Why are we using e?

    Reply
    1. Nancy Agrawal

      To write the code of clear button add a Clear button as
      Button b6 = new Button();

      Then set it into the frame as:
      b6.setBounds(350, 250, 50, 20);

      Then write:
      b6.addActionListener(this);

      Then add it to the frame as:
      f.add(b6);

      Then write in actionPerformed(ActionEvent e) method:
      if(e.getSource() == b6)
      {
      t1.setText(” “);
      t2.setText(” “);
      t3.setText(” “);
      }

      Reply

Leave a Reply

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