JAVA APPLET Program to Find Sum of Two Numbers

At the time creation of a data base, we must embed the applet into the HTML page then only it works

Step 1: Create the java program with " filename.java ".

Step 2: Create the html program with "filename.html".

Step 3: Compile the java program "javac filename.java".

Step 4: View applet using "appletviewer filename.html".

Note: Both program must have the same name and Both program must be in same folder.

Java Program

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.applet.*;  
  4. public class Q2 extends Applet implements ActionListener  
  5. {  
  6.     TextField t1 = new TextField(10);  
  7.     TextField t2 = new TextField(10);  
  8.     TextField t3 = new TextField(10);  
  9.     Label l1 = new Label("FIRST NO=:");  
  10.     Label l2 = new Label("SECOND NO:");  
  11.     Label l3 = new Label("SUM:");  
  12.     Button b = new Button("ADD");  
  13.     public void init()  
  14.     {  
  15.         t1.setForeground(Color = Red);  
  16.         add(l1);  
  17.         add(t1);  
  18.         add(l2);  
  19.         add(t2);  
  20.         add(l3);  
  21.         add(t3);  
  22.         add(b);  
  23.         b.addActionListener(this);  
  24.     }  
  25.     public void actionPerformed(ActionEvent e)  
  26.     {  
  27.         if (e.getSource() == b)  
  28.         {  
  29.             int n1 = Integer.parseInt(t1.getText());  
  30.             int n2 = Integer.parseInt(t2.getText());  
  31.             t3.setText(" " + (n1 + n2));  
  32.         }  
  33.     }  
  34. }  
HTML Code
  1. <HTML>  
  2.   
  3.     <HEAD>  
  4.         <TITLE>WELCOME TO JAVA APPLET</TITLE>  
  5.     </HEAD>  
  6.   
  7.     <BODY>  
  8.         <CENTER>  
  9.             <H1>WELCOME TO THE APPLET</H1> </CENTER>  
  10.         <BR>  
  11.         <APPLET CODE=Q2.class WIDTH=400 HEIGHT=400> </APPLET>  
  12.     </BODY>  
  13.   
  14. </HTML>  

OUTPUT