Calculator
Calculator
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
add(l1);
l1.setBounds(30,30,65, 30);
setForeground(Color.black);
add(t1);
t1.setBounds(110,30,100, 30);
setForeground(Color.white);
add(l2);
setForeground(Color.black);
l2.setBounds(30,75,65, 30);
add(t2);
t2.setBounds(110,75,100, 30);
add.setBackground(Color.red);
add(add);
add.setBounds(110, 120, 100,35);
sub.setBackground(Color.green);
add(sub);
sub.setBounds(110, 160, 100,35);
mul.setBackground(Color.blue);
add(mul);
mul.setBounds(110, 200, 100,35);
div.setBackground(Color.cyan);
add(div);
div.setBounds(110, 240, 100,35);
clr.setBackground(Color.orange);
add(clr);
clr.setBounds(110, 280, 100,35);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
clr.addActionListener(this);
}
if(e.getSource()== sub)
{
str = "Substraction is ";
result = num1-num2;
repaint();
}
if(e.getSource()== mul)
{
str = "Multiplication is ";
result = num1*num2;
repaint();
}
if(e.getSource()== div)
{
str = "Division is ";
result = (num1/num2);
repaint();
}
if(e.getSource()==clr)
{
t1.setText("");
t2.setText("");
str = "Answer : ";
result = 0;
repaint();
}
}
}