Advertisement
| Constructor | Description |
|---|---|
| public Label() | Creates a Label with an empty text. |
| public Label(String text) | Creates a Label with an specified text. |
| public Label(String text, int alignment) | Creates a Label with an specified text and alignment. |
| Methods | Description | public void setText(String text) | Sets a String message on the Label. |
|---|---|
| public String getText() | Gets a String message of Label. |
| void setAlignment(int alignment) | Sets an alignment on this label. |
| void int getAlignment() | Gets the current alignment of this label |
import javax.swing.*;
import java.awt.*;
public class LabelEx1
{
Frame jf;
Label label1, label2, label3;
LabelEx1()
{
jf = new Frame("Label example");
label1 = new Label("Hi!");
label2 = new Label("Bonjour, how are you?");
label3 = new Label("We wish you pleasant day ahead, Take care!");
label1.setText("Welcome and Hello!"); //Setting the text of a JLabal
jf.setLayout(new FlowLayout());
jf.add(label1); //Adding label1 to the Jframe
jf.add(label2); //Adding label2 to the Jframe
jf.add(label3); //Adding label3 to the Jframe
jf.setVisible(true);
jf.setSize(300,300);
}
public static void main(String... ar)
{
new LabelEx1();
}
}
Figure 1 Advertisement
import javax.swing.*;
import java.awt.*;
public class LabelEx2
{
Frame jf;
Label label1, label2, label3;
LabelEx2()
{
jf = new Frame("Label example");
label1 = new Label("NORTHEAST", Label.RIGHT);
label2 = new Label("SOUTHWEST", Label.LEFT);
label3 = new Label("CENTRAL", Label.CENTER);
//("Hello!"); //Setting the text of a JLabal
jf.add(label1, BorderLayout.NORTH); //Adding label1 to the Jframe
jf.add(label2, BorderLayout.SOUTH); //Adding label2 to the Jframe
jf.add(label3, BorderLayout.CENTER); //Adding label3 to the Jframe
jf.setVisible(true);
jf.setSize(300,300);
}
public static void main(String... ar)
{
new LabelEx2();
}
}
When you run the code, you are presented a window shown in the Figure2 below -:
Figure 2
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement