|
Calculator
import java.awt.*;
public class Parents extends Frame
{
CheckboxGroup opt;
Checkbox adt, sub, mul, div;
TextField no1,no2;
Label nu1,nu2,opr;
Button ok;
String message;
public Parents()
{
setTitle ("Calculator") ;
opt = new CheckboxGroup();
adt = new Checkbox("Add",opt,false);
sub = new Checkbox("Sub",opt,false);
mul = new Checkbox("Mul",opt,false);
div = new Checkbox("Div",opt,false);
no1 = new TextField();
no2 = new TextField();
nu1 = new Label("First Number ");
nu2 = new Label ("Second Number") ;
opr = new Label("Operations ");
ok = new Button ("Perform") ;
add(no1);
add(nu1);
add(no2);
add(nu2);
add(opr);
add(adt);
add(sub);
add(mul);
add(div);
add(ok);
nu1.reshape(200,50,100,50);
no1.reshape(325,50,100,50);
nu2.reshape(200,100,100,50);
no2.reshape(325,100,100,50);
opr.reshape(10,50,100,50);
adt.reshape(30,100,100,25);
sub.reshape(30,125,100,25);
mul.reshape(30,150,100,25);
div.reshape(30,175,100,25);
ok.reshape(400,900,100,50);
public void paint(Graphics g)
{
g.drawString(message,50,300);
}
public boolean handleEvent(Event e)
{
if (e.id == Event.WINDOW_DESTROY)
{
System.exit(0);
return super.handleEvent(e);
}
public boolean action(Event e,Object arg)
{
if (arg.equals(oPerformo))
{
double result = 0.0;
if (adt.getState())
result = (float) Integer.parseInt(no1.getText())
Integer.parseInt(no2.getText());
if (sub.getState())
result = (float) Integer.parseInt(no1.getText())
Integer.parseInt(no2.getText());
if (mul.getState())
result = (float) Integer.parseInt(nol.getText())
Integer.parseInt(no2.getText());
if (div.getState())
result = (float) Integer.parseInt(no1.getText())/Integer.parseInt(no2.getText());
message = "The result is " + result;
repaint();
return super.action(e,arg);
public static void main(String arg[])
{
Parents p;
p = new Parents();
p.show();
p.resize(600,600);
}
}
Output
:
This code will display Calculator.
|