Creating Labels in an Applet Window using Advanced Java Programming

Home  »  Java  »  Advance Java »    Java AWT (Abstract Window ToolKit) »    Labels


Label is used for displaying a Description Which Cant' be change at the Run Time and which doesn't take any input from a user and used for which is used for displaying a text string that is a un-editable and for creating a label First Create the object of Label Class.

  1. Label(): Empty label or doesn't display any String on Label.
  2. Label(String txt): with a text string displayed on Label.
  3. Label(String txt, int align): which is used for first displaying the string and then its alignment using Label. LEFT or 0,1,2 etc. for ex:
Label L1= new Label("Name")
add(L1);

It first creates a label with a description or text named as Name and have a Alignment left then it add this label to applet or Frame .For Specifying the Alignment we can also Specify the Number or either can Write Label.RIGHT etc. same for all Alignments Like Left and Center.

Values
Description
Label.RIGHTAligns label to the right of the applet window.
Label.CENTERAligns label to the center of the applet window.
Label.LEFTAligns label to the left of the applet window.

Note : Labels are aligned to the center by default if the alignment is not specified.

You can use the methods available in the Font and Color classes to apply formatting and colour to the Label objects. The following code shows how to create labels:

Creating Labels in an Applet Window:

The above code will be stored in the file called LabelTest.java, which on compilation will create a class file called LabelTest.class. Now compiple this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac LabelTest.java
C:\>jdk1.4\bin>appletviewer LabelTest.java


"Output of LabelTest.java"


Download Complete Program

  -  



The methods of a labels:-
  1. void setText() : To Enter the Text in a Label at any Time.
  2. voidsetText(String) : Displaying a String on the Label.
  3. voidsetAlignment(int align) : Specify the Alignment of Label depends upon the value means 0 for left ,1 for center and 2 for Right.
  4. int getAlignment() : Used to Retrieve the Current Alignment of Label Whether it is Left , Center or in the Right Side.




AWT Controls



Label Buttons Check Boxes Radio Buttons
Choice Controls TextFields & Password TextAreas Scrolling Lists Scrollbars




No comments:

Post a Comment