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

 

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

    1. 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.

  1. 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);

    1. 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 ↓

    1. 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;

  2. 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);

    1. 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(” “);
      }

  3. Sir I got this dialogue..can u clarify?

    Exception in thread “main” java.awt.HeadlessException:
    No X11 DISPLAY variable was set, but this program performed an operation which requires it.
    at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
    at java.awt.Button.(Button.java:152)
    at Calc.(Calc.java:5)
    at Calc.main(Calc.java:56)

  4. $javac Calculator.java
    Calculator.java:12: error: cannot find symbol
    this.setsize(400,400);
    ^
    symbol: method setsize(int,int)
    Calculator.java:13: error: cannot find symbol
    this.setTitle(“Simple Calculator”);
    ^
    symbol: method setTitle(String)
    Calculator.java:37: error: cannot find symbol
    setLayout(null);
    ^
    symbol: method setLayout()
    location: class Calculator
    Calculator.java:38: error: cannot find symbol
    this.add(l1);
    ^
    symbol: method add(Label)
    Calculator.java:39: error: cannot find symbol
    this.add(l2);
    ^
    symbol: method add(Label)
    Calculator.java:40: error: cannot find symbol
    this.add(l3);
    ^
    symbol: method add(Label)
    Calculator.java:41: error: cannot find symbol
    this.add(t1);
    ^
    symbol: method add(TextField)
    Calculator.java:42: error: cannot find symbol
    this.add(t2);
    ^
    symbol: method add(TextField)
    Calculator.java:43: error: cannot find symbol
    this.add(t3);
    ^
    symbol: method add(TextField)
    Calculator.java:44: error: cannot find symbol
    this.add(b1);
    ^
    symbol: method add(Button)
    Calculator.java:45: error: cannot find symbol
    this.add(b2);
    ^
    symbol: method add(Button)
    Calculator.java:46: error: cannot find symbol
    this.add(b3);
    ^
    symbol: method add(Button)
    Calculator.java:47: error: cannot find symbol
    this.add(b4);
    ^
    symbol: method add(Button)
    Calculator.java:48: error: cannot find symbol
    this.add(b5);
    ^
    symbol: method add(Button)
    Calculator.java:55: error: cannot find symbol
    setVisible(true);
    ^
    symbol: method setVisible(boolean)
    location: class Calculator
    15 errors

    please help to solve this

    1. i have modify bit as per my class work needed to it be a part of my college program but keep getting errors

  5. Kiprotich philip

    What is the best easiest way of setting bounds I don’t understand how the x,y, width, height values change for label and textfield

Leave a Comment

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