Label Methods

Labels are simple objects which have only a few constructors and methods of their own, separate from the general methods of java.awt.Component (which java.awt.Label subclasses).

  public final static int LEFT
  public final static int CENTER
  public final static int RIGHT
  public Label()
  public Label(String text)
  public Label(String text, int alignment)
  public void addNotify()
  public int getAlignment()
  public synchronized void setAlignment(int alignment)
  public String getText()
  public synchronized void setText(String text) 
You've already seen the basic constructor for a Label. You can also create a Label with no text at all using the Label() constructor with no arguments. There's little to no reason to do this. You can also define that a Label is right, left, or center aligned by passing the approriate constant to the constructor:

Label center = new Label("This label is centered", Label.CENTER);
Label left = new Label("This label is left-aligned", Label.LEFT);
Label right = new Label("This label is right-aligned", Label.RIGHT);
The two methods of java.awt.Label which you may occasionally have reason to call are getText() and setText(String s). These allow you to retrieve and change the text of a Label while the applet is running. Given a Label l here is how they might be used

String s = l.getText();
l.setText("Here's the new label");

Previous | Next | Top
Last Modified March 4, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu