Apr 18, 2012

Label


The simplest AWT component is label. Labels do not respond to the user inputs, and they do not send any other events.
 The following constructors are used to create a label.

Label()
Label(String caption)
Label(String caption, alignment)

The first constructor creates a empty label. The second one creates a specified text in the label. The default alignment to the label is left. The third constructor is used for creating a label with your alignment. The alignment values are:

                 Label.CENTER
         Label.RIGHT
         Label.LEFT

There are two methods supports the reading and setting text for the label.

getText()    - It returns the text from the Label
setText()    - It set the text for the Label 

Example:


import java.applet.*;
import java.awt.*;
public class LabelDemo extends Applet
{
public void init()
        {
             setLayout(new GridLayout(2,1));
             setBackground(Color.yellow);
             Label l1=new Label("Java Programming");
             Label l2=new Label("by surendranath Reddy",Label.CENTER);
             add(l1);
             add(l2);
        }
}

/*
<applet code="LabelDemo.class" width=200 height=75>
</applet>
*/

How to Execute this Example??

d:\>  javac LabelDemo.java
d:\>  appletviewer LabelDemo.java

Output:






0 comments :

Post a Comment