Java Program to create Arithmetic Math Calculator Using Applet Class ant Event Handling

Write a Java Program to create Arithmetic Math Calculator Using Applet Class ant Event Handling:-

import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
public class calc extends Applet implements ActionListener
{ Button add,sub,divide,multi;
Label result,no1,no2;
TextField tf,ip1,ip2;
Panel p1,p2,p3;
public void init()
{
add=new Button("ADD");
sub=new Button("SUBTRACT");
divide=new Button("DIVIDE");
multi=new Button("MULTIPLY");
result = new Label("Result = ");
no1=new Label ("NUMBER 1:");
no2=new Label ("NUMBER 2:");
tf=new TextField(20);
ip1=new TextField(10);
ip2=new TextField(10);
p1=new Panel();
p2=new Panel();
p3=new Panel();
tf.setEditable(false);
add.setSize(20,40);
sub.setSize(20,40);
divide.setSize(20,40);
add.addActionListener(this);
sub.addActionListener(this);
divide.addActionListener(this);
multi.addActionListener(this);
setLayout(new FlowLayout());
p1.add(no1);
p1.add(ip1);
p1.add(no2);
p1.add(ip2);
p2.add(add);
p2.add(sub);
p2.add(divide);
p2.add(multi);
p3.add(result);
p3.add(tf);
add(p1);
add(p2);
add(p3);
setSize(400,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ int a,b;
int result;
a =Integer.parseInt(ip1.getText());
b=Integer.parseInt(ip2.getText());
if(e.getSource()==add)
{System.out.println("ADD");
result=(a+b);
tf.setText("Addition :"+String.valueOf(result));
}
if(e.getSource()==sub)
{
result=(a-b);
tf.setText("Subtraction : "+String.valueOf(result));
}
if(e.getSource()==multi)
{
result=(a*b);
tf.setText("Multiplication : "+String.valueOf(result));
}
if(e.getSource()==divide)
{
try{
if(b==0)
{
result=(a/b);
tf.setText("Division :"+String.valueOf(result));
}
}
catch(ArithmeticException ae )
{
tf.setText("Division can't be performed");
}
}
}
}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics