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.

```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 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);

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.

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:

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:

1. modify main as

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

{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

import java.awt.event.WindowListener;

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

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

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:

Then add it to the frame as:

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

3. how did u set the cordinates
that setbounds
for what did u give those commands
can u explain?

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

No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.Button.(Button.java:152)
at Calc.(Calc.java:5)
at Calc.main(Calc.java:56)

5. I cannot understand how you have set coordinates in this program. Can you help me to understand this?

6. invalid method decleration ; return type required
is the error shown…
can anyone help me with this

7. \$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
^
Calculator.java:39: error: cannot find symbol
^
Calculator.java:40: error: cannot find symbol
^
Calculator.java:41: error: cannot find symbol
^
Calculator.java:42: error: cannot find symbol
^
Calculator.java:43: error: cannot find symbol
^
Calculator.java:44: error: cannot find symbol
^
Calculator.java:45: error: cannot find symbol
^
Calculator.java:46: error: cannot find symbol
^
Calculator.java:47: error: cannot find symbol
^
Calculator.java:48: error: cannot find symbol
^
Calculator.java:55: error: cannot find symbol
setVisible(true);
^
symbol: method setVisible(boolean)
location: class Calculator
15 errors