Three Steps to Adding a Component

   public void init() {
     Label l;
     l = new Label("Hello Container");
     this.add(l);
   }
The key thing to remember about adding components to the applet is the three steps:

  1. Declare the component
  2. Initialize the component
  3. Add the component to the layout.
The first two steps must be performed when creating an instance of any class so it's really only the third that's new.

You can often combine the three steps into one like this

this.add(new Label("Hello Container"));

The disadvantage to this shortcut is that you no longer have a variable which references the Label. Thus you can't easily access the Label later. However labels are fairly constant things, so you're unlikely to want to access it anyway.


Previous | Next | Top
Last Modified October 21, 1999
Copyright 1997, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu