/*
Create AWT Label With Text Alignment Example
This java example shows how to create a label and align label text using
AWT Label class.
*/
import java.applet.Applet;
import java.awt.Label;
/*
<applet code="CreateLableWithAlignment" width=100 height=200>
</applet>
*/
public class CreateLableWithAlignment extends Applet{
public void init(){
/*
* To create a label with it's text alignment use
* Label(String text, int alignment)
* constructor of AWT Label class.
*
* The possible alignment values are
* Label.LEFT, Label.RIGHT, and Label.CENTER
*/
Label label1 = new Label("Left Aligned Label", Label.LEFT);
Label label2 = new Label("Right Aligned Label", Label.RIGHT);
Label label3 = new Label("Center Aligned Label", Label.CENTER);
//add labels
add(label1);
add(label2);
add(label3);
/*
* To change alignment of label's text use
* void setAlignment(int alignment)
* method of AWT Label class.
*/
Label label4 = new Label("Set Center Alignment");
add(label4);
label4.setAlignment(Label.CENTER);
}
}
Example Output

Bookmark/Search this post with: