Labels

The simplest component is java.awt.Label. A Label is one line of read-only text, pretty much perfect for a Hello World applet.

import java.applet.*;    
import java.awt.*;
 
public class HelloContainer extends Applet {
 
  public void init() {
    Label label;
    label = new Label("Hello Container");
    this.add(label);
  }

}
Hello Container applet

As usual you begin by importing the classes you need. In this case you need only two, java.applet.Applet and java.awt.Label and lines 1 and 2 import them.

Line 4 declares the class in the usual way as an extension of Applet. The class has a single method, init().

Line 6 starts the init() method. The init() method does three things. First line 7 declares that l is a Label. Then l is instantiated with the Label(String s) constructor in Line 8. Finally l is added to the layout in line 9. Components don't have to be added to the layout in the init() method nor do they need to be instantiated there, but it's often convenient to do so.


Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998, 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified Auguust 2, 2002