Creating and Running Applet with AWT Components


Step 1: Create a Source file named "testapp.java"

Step 2: Write the following code.

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

/*
  <applet code="testapp" height=140 width=320 align="center">
 </applet>
*/
public class testapp extends Applet implements ActionListener
{
    Label l1,l2,l3,l4;
    Button ok,cancel;
    TextField name,add;
    public void init()
     { 
       setBackground(new Color(219,240,251));
        setLayout(null);
       name=new TextField(15);
      add=new TextField(15);      
       l1=new Label("Enter Your Name:");
       l1.setBounds(5,10,130,30);
       name.setBounds(150,10,130,20);
       l2=new Label("Enter Your Address:");
       l2.setBounds(5,40,130,30);     
       add.setBounds(150,40,130,20);
       l3=new Label();
       l4=new Label("Your Name & Address is : ");
       ok=new Button("Ok");
       cancel=new Button("Clear");
       ok.setBounds(80,100,70,35);
       cancel.setBounds(180,100,80,35);
       l4.setBounds(5,70,150,30);
       l3.setBounds(162,70,180,30);
       add(l1);  
       add(name);
       add(l2); 
       add(add);
       add(l3);
       add(l4);
       add(ok);
       add(cancel);
      ok.addActionListener(this);
      cancel.addActionListener(this);
     } 
 public void actionPerformed(ActionEvent ae)
   {
       if(ae.getSource()==ok) 
         {
            String str=name.getText();
            str=str+" ,  "+add.getText();  
           l3.setText(str);     
         }
     else
        { 
          name.setText("");
          add.setText("");
          l3.setText("");
        }
  }
}

Step 3 : Compile the source file.

Step 4: Run using Appletviewer tool

             appletviewer testapp.java

Output :


Run using html file.

Write html code in a html file.
<html>
<head> <title>Applet Example </title></head>
<body>
<h2>This is an Example of Applet with AWT Components  </h2>
  <applet code="testapp" height=140 width=320 align="center">
 </applet>
</body>
</html>

Output :


Now Enjoy !  and give feedback (if any)

Comments

Post a Comment

Popular posts from this blog

JDatePicker In Java

Setting Background image of JFrame containg Components

Applet Progarm with Combo box, radio button,Check box