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

 

import java.awt.*;
import java.awt.event.*;

class Calculator 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");
	
	Calculator()
	{
		//Giving Coordinates
		l1.setBounds(50,100,100,20);
		l2.setBounds(50,140,100,20);
		l3.setBounds(50,180,100,20);
		
		t1.setBounds(200,100,100,20);
		t2.setBounds(200,140,100,20);
		t3.setBounds(200,180,100,20);
		
		b1.setBounds(50,250,50,20);
		b2.setBounds(110,250,50,20);
		b3.setBounds(170,250,50,20);
		b4.setBounds(230,250,50,20);
		b5.setBounds(290,250,50,20);
		
		//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(400,350);
	}
	
	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 Calculator();
	}
}

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 *