Categories
Uncategorized

Java Applets




Write a program to add two numbers using lebel and textfield in a java applets.

import java.awt.*;
import java.applet.*;


public class add extends Applet
{
TextField t1,t2;
Label l1;
public void init()
{
l1=new Label(“Enter the num to add”);
t1=new TextField(10);
t2=new TextField(10);
add(l1);
add(t1);
add(t2);
}


public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;
try{
s1=t1.getText();
x=Integer.parseInt(s1);
s2=t2.getText();
y=Integer.parseInt(s2);
}catch(Exception e){}
z=x+y;
//s=String.valueof(z);
g.drawString(“THE SUM IS:”+z,50,70);
//g.drawString(s,150,70);
}
public boolean action(Event event,Object object)
{
repaint();
return true;
}
}

//<html>
//<applet code =add.class width=300 height=200>
//</applet>
//</html>

Note: With the help of this code you can even try for multiplication,division for the two numbers.

References: Programming in java, E Balagurusamy