Showing posts with label label in java awt. Show all posts
Showing posts with label label in java awt. Show all posts

Friday, 15 February 2019

Label class in Java AWT - Online Help

Label class in java AWT:

              Label class in java AWT is nothing but a class can be used to add a text in the table. The object of label class is a component for placing text in a container. It is used to display a single line of read-only text. The label is a passive control because it does not create any event when accessed by the user. The text can be changed by an application but a user cannot edit it directly. 

A constructor of java AWT Label class:

      There are three types of constructor available in the label class. They are:

Label():
It is used to create an empty label.

Label(String str)
it is used to create a label with the specified string.

Label(String str,int align)
 it is used to create a label with the specified string and the alignment.

The possible alignment is as follow:

Label.CENTER- display a  string in the label with CENTER ALIGNMENT
Label.LEFT-display a  string in the label with LEFT ALIGNMENT
Label.RIGHT-display a  string in the label with RIGHT ALIGNMENT

Methods available in java AWT Label:

The most commonly used method is as follows:

1.void setText(String str)
This method is used to set the text for the label object.

2.void setAlignment(int align)
This method is used to set the alignment of the label content.

3.void addNotify()
Creates the peer for this label.

4.AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Label.

5.int getAlignment()
Gets the current alignment of this label.

6.String getText()
Gets the text of this label.

7.protected String paramString()
Returns a string representing the state of this Label

8.void setBackground(color bgcolor)
This method is used to set the color of the background for the label.

9.void setForeground(color strcolor)
This method is used to set the color of the string for the label.

Sample Program for label class in java AWT: 

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="labeldemo" width=500 height=500>
</applet> */
public class labeldemo extends Applet
{
Label l1;
Label l2;
Label l3;
public void init()
{
l1=new Label("welcome to online help",Label.LEFT);
add(l1);
l2=new Label("welcome to online help",Label.RIGHT);
add(l2);
l3=new Label("welcome to online help",Label.CENTER);
add(l3);
}
}