Wednesday, 1 October 2014

Program for taking UserInput in Applet


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

public class UserInput extends Applet
{
      TextField text1, text2;

      public void init()
      {
           text1 = new TextField("",10);
           text2 = new TextField(8);
           text1.setBounds(100,100,50,50);
           add(text1);
           add(text2);
           text1.setText("");
           text2.setText("");
      }

      public void paint(Graphics g)
      {
            int x = 0,y = 0,z = 0;
            String str1 = "0",str2 = "0",sum = "0";
            g.drawString("Input a number in each box: ",5,20);
            try
            {
                    str1 = text1.getText();
                    x = Integer.parseInt(str1);
                    str2 = text2.getText();
                    y = Integer.parseInt(str2);
            }
            catch(Exception e)
            {
 
            }

            z = x + y;
            sum =  String.valueOf(z);
            g.drawString("The Sum is: ",5,50);
            g.drawString(sum,95,50);
    }

    public boolean action(Event event, Object obj)
    {
          repaint();
          return true;
    }
}

/*


*/
Output