Java Reference
In-Depth Information
In this chapter, I will show you how to write an applet and embed it in an
HTML page. This will involve writing some HTML, but don't worry if you are
not familiar with it. Only a little HTML is needed, and I promise to keep to the
basics. JAR files will also be discussed in detail because they are important
aspects of applets. I will begin with a discussion on the Applet class, the start-
ing point for writing an applet.
The java.applet.Applet Class
An applet is a Java class; if the applet is to be viewed in a Web browser, the
class must extend the java.applet.Applet class. The Applet class provides a
common interface so that a Web browser can communicate with the applet.
An interesting note about the Applet class is that it extends java.awt.Panel.
This means that an applet is a panel, which is a java.awt.Container; therefore,
an applet can have components added to it just like any container, as well as
have a layout manager assigned to it, and you can even nest panels within an
applet to create the GUI you want.
An applet has FlowLayout by default, but any layout manager can be
assigned to an applet using the setLayout() method.
Let's take a look at a simple Applet class. The following HelloWorldApplet
extends applet and adds a button to the applet. The event handling is done in
the ensuing PrintHello class. Study the following code, and try to determine
what this applet does.
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet
{
private Button go;
private TextField name;
private Label hello;
public void init()
{
go = new Button(“Go”);
name = new TextField();
hello = new Label(“”, Label.CENTER);
this.setLayout(new BorderLayout());
this.add(name, BorderLayout.NORTH);
Panel center = new Panel();
Search WWH ::




Custom Search