Saturday, July 20, 2013


Label is a component in AWT package. It is used to display non-editable text on the window. You can create a label by using the Label class. The Label class is summarized below.

Label constructors:
creates a label object without any text on it.
-Label(String text)
creates a label object with text on it.
-Label(String text,int alignment)
creates a label object with text on it and the text alignment is specified.

Useful fields of the Label class:
Useful methods of the Label class:
reads text from the label object.
-setAlignment(int alignment)
sets the text alignment of the label object.
-setText(String text)
sets the text of the label object.
-setForeground(Color color)
sets the text color the label object.
-setFont(Font font)
sets the font of text of the label object.

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
InterfaceShow(String title){
setSize(new Dimension(500,300));

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){

Label lbl=new Label();
Lbl.setText("This is the text placed on the Label.");
lbl.setFont(new Font("Arial",Font.BOLD,20));
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using Label");


Label in awt

In the example code about, a label object called lbl is created without specifying its text and with the default alignment. The the setText method is used to set the text of the label. Its setFont method is used to set a font object to text of the label. The syntax to create a font object is Font f=new Font(Font_name, Font_style, Font_size). To specify that the text of the label must be left aligned, you need to use the setAlignment method. This method accepts a constant value Label.LEFT, Label.RIGHT, or Label.CENTER. If you supply Label.LEFT to this method, the text of the label will be left aligned. By supplying Label.RIGHT, the text is right aligned and so son.

1 comment:

  1. Hmm it looks like your blog ate my first comment (it was super long) so I guess I'll just sum it up what I submitted and say, I'm thoroughly enjoying your blog. I too am an aspiring blog writer but I'm still new to the whole thing. Do you have any suggestions for novice blog writers? I'd genuinely appreciate it.List of Media Companies in Singapore