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.
Also Read: How to Make a Calculator in Java Using Swing
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(); } }
PLEASE WHAT IS AWT,and thanks for the code but enligthen me further..
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.
THANK FOR YOUR EXPLANATION
Thank u so much such a great job
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);
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 ↓
sir i have to no run any java Applet so plz suggests?
Type class Calculator extends Frame implements Action Listener {
Declare Frame f;
How can I remove the cancel option to close and allow the red x to close
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;
Return Type required-Calculator()
What to do??
I am a Beginner
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);
no its right if you have cut pasted the code you should change that comment section
here,what is use of (String.valueOf)
It converts the value into String.
What is the significance of ActionEvent e ?Why are we using e?
Sir How to write code for clear button
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(” “);
}
how did u set the cordinates
that setbounds
for what did u give those commands
can u explain?
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)
I cannot understand how you have set coordinates in this program. Can you help me to understand this?
how to Compile it
invalid method decleration ; return type required
is the error shown…
can anyone help me with this
$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
i have modify bit as per my class work needed to it be a part of my college program but keep getting errors
Please explain the program once
a brief explanation of this code for me to explain it
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