SumApplet.java

Aim:
To find sum of two numbers using AWT & Applet class

Source code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SumApplet extends Applet implements ActionListener
 {
   int a,b,sum;
   Label l1=new Label("a value");
   TextField t1=new TextField(5);
   Label l2=new Label("b value");
   TextField t2=new TextField(5);
   Button b1=new Button("sum");
   public void init()
     {
       add(l1); add(t1); add(l2); add(t2); add(b1);
       b1.addActionListener(this);
     }
   public void paint(Graphics g)
     {
       g.drawString("Sum of two numbers="+sum,20,70);
      }
   public void actionPerformed(ActionEvent e)
     {
       a=Integer.parseInt(t1.getText());
       b=Integer.parseInt(t2.getText());
       sum=a+b;
       repaint();
     }
 }
// <applet code="FirstApplet.class" height=200 width=200> </applet>


Output:

 

No comments:

Post a Comment