Skip to main content

Sample: A Java Applet Program to Calculate Summation of two numbers

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

public class user extends Applet
{
    TextField t1,t2; // Declaring Object variables
    public void init() // Initializing Objects
    {
        t1=new TextField(10);
        t2=new TextField(10);
        add(t1);
        add(t2);
        t1.setText("0.00");
        t2.setText("0.00");
    }

    public void paint(Graphics g) // Drawing Objects On The Java Form
    {
        int x=0,y=0,z=0;
        String s1,s2,s;
        g.drawString("Input A number in Each Box",10,50);
        try
        {
            s1=t1.getText();
            x=Integer.parseInt(s1);
            s2=t2.getText();
            y=Integer.parseInt(s2);
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }

        z=x+y;
        s=String.valueOf(z);
        g.drawString("THE SUM IS :: ",10,75);
        g.drawString(s,100,75);
    }

    public boolean action(Event e,Object o) //On Enter Key Pressing The Paint() is recalling Here
    {
        repaint();
        return true;
    }
}


Comments