Sunday, May 15, 2011

Java's Applet

An Applet is a small utility program of Java that requires minimal resources. Normally, you run an applet in a bigger program like in your own Java program or web systems.

You can run an applet in two ways:

  • using an Applet viewer that goes with your Java software
  • using an HTML page

An applet is created quite similar to creating an ordinary Java program but we only use the parent class Applet to support the applet application. Further, we can include GUI components and events into our applet.  The program below illustrates this use.

[+/-] show/hide

* @author Rose
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class AppletGreetEvent extends Applet implements ActionListener {
JLabel greeting1 = new JLabel("Hello. Who are you?");
JLabel greeting2 = new JLabel();
Font bigFont = new Font("Times New Roman", Font.ITALIC,24);
JButton pressMe = new JButton("Press Me");
JTextField answer = new JTextField(" ",10);

public void init()

answer.requestFocus();// focus for the textfield

public void start()
{ greeting2.setText("This is a sample applet.");}

public void actionPerformed(ActionEvent thisEvent)
String name=answer.getText();

greeting2.setText("Hi "+name);

No comments: