Usage

The JLabel class can be used to add text (and icon) labels to a container. 

Package

Javax.swing

Class

javax.swing.JLabel

Parent Classes

javax.swing.JLabel implements SwingConstants, Accessible
javax.swing.JComponent implements Serializable
java.awt.Container
java.awt.Component implements ImageObserver, MenuContainer, Serializable
java.lang.Object

Common Public Constructors

JLabel ()
Creates a blank JLabel instance.
JLabel (String Text)
Creates a JLabel instance with the specified text.
JLabel (String Text, int Alignment)
Creates a JLabel instance with the specified text and horizontal alignment.

Common Public Methods

void setBackground (Color BgdColor)
Sets the color of the background for the label.
void setHorizontalAlignment (int Alignment)
Sets the alignment of the label's contents along the X axis.
void setFont (Font TextFont)
Sets the font for this component.Arguments
void setForeground (Color TextColor)
Sets the color of the text for the label.
void setText (String Text)
Sets the text for the label.

Arguments

Alignment
The horiziontal position of the text within the label.  One of:
JLabel.LEADING
Identifies the leading edge of text for use with left-to-right and right-to-left languages.
JLabel.CENTER
The central position in an area.
JLabel.TRAILING
Identifies the trailing edge of text for use with left-to-right and right-to-left languages.
JLabel.LEFT
Box-orientation constant used to specify the left side of a box.
JLabel.RIGHT
Box-orientation constant used to specify the right side of a box.
BgdColor
The color to be used for the background of the label.
Text
The text to appear in the label.
TextColor
The color to be used for the text of the label.
TextFont
The font to be used for the text of the label.

Applet - Label1.class

Source Code - Label1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Label1 extends JApplet {
  /* Declaration */
  private Container Panel;
  private LayoutManager Layout;
  private JLabel Label1;
  private JLabel Label2;
  private JLabel Label3;
  private JLabel Label4;
  private JLabel Label5;
  private JLabel Label6;
  private JLabel Label7;

  public Label1 () {
    /* Instantiation */
    Layout = new GridLayout (7, 1);
    Label1 = new JLabel ("A Simple Label");
    Label2 = new JLabel ("A Label with LEFT alignment", JLabel.LEFT);
    Label3 = new JLabel ("A Label with CENTER alignment", JLabel.CENTER);
    Label4 = new JLabel ("A Label with RIGHT alignment", JLabel.RIGHT);
    Label5 = new JLabel ("A Label with LEADING alignment", JLabel.LEADING);
    Label6 = new JLabel ("A Label with TRAILING alignment", JLabel.TRAILING);
    Label7 = new JLabel ();
    Panel = getContentPane ();
    
    /* Location */
    Panel.setLayout (Layout);
    Panel.add (Label1);
    Panel.add (Label2);
    Panel.add (Label3);
    Panel.add (Label4);
    Panel.add (Label5);
    Panel.add (Label6);
    Panel.add (Label7);

    /* Decoration */
    Panel.setBackground (Color.yellow);
    Label7.setHorizontalAlignment (JLabel.CENTER);
    Label7.setForeground (Color.blue);
    Label7.setText ("Text added with setText");
  }

}

Reference Links

Swing Controls Sampler
AWT Label Examples

External Links

Sun Java site How to Use Labels
Sun Java site Class JLabel API