Table of contents
1.
Introduction
2.
What is Label in Java AWT?
2.1.
AWT Java Label Syntax
3.
Java AWT Label Fields
4.
Java AWT Label Class Constructors
5.
AWT Java Label Class Methods
6.
Java AWT Label Examples
7.
Frequently Asked Questions
7.1.
What is the label in Java?
7.2.
How to label a statement in Java?
7.3.
What is a label and a button?
8.
Conclusion
Last Updated: Aug 5, 2024
Easy

Java AWT Label

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Label class object is a component for inserting text into a container. It's used to show a single line of text that can only be read. A programmer can edit the text, but a user cannot edit it directly.

Java AWT Label

It's termed passive control since it doesn't do anything when activated. To make a label, we must first build a Label class object.

Let us now understand the AWT label in detail, with the help of examples.

What is Label in Java AWT?

In Java AWT (Abstract Window Toolkit), a Label is a component used to display a short, uneditable piece of text or an image. It provides a simple way to add descriptive text or graphical content to a user interface. Labels are commonly used in forms, dialogs, and other GUI components to provide information to the user. They can display text in various fonts, sizes, and colors, and can also include images or icons for visual representation. Labels do not interact with user input and are primarily used for display purposes.

AWT Java Label Syntax

public class Label extends Component implements Accessible  

Java AWT Label Fields

The following fields are available in the java.awt.Component class:

static int LEFT: It mandates that the label is justified to the left.

static int RIGHT: It mandates that the label be justified to the right.

static int CENTER: It directs that the label be positioned in the center.

Java AWT Label Class Constructors

The table below lists several types of Label class constructors.

Constructor Description
Label() Constructs a new label with no text.
Label(String text) Constructs a new label with the specified text.
Label(String text, int alignment) Constructs a new label with the specified text and alignment. The alignment parameter can be one of Label.LEFT, Label.CENTER, or Label.RIGHT.

AWT Java Label Class Methods

Method Description
void setText(String text) Sets the text of the label to the specified string.
String getText() Retrieves the current text of the label.
void setAlignment(int alignment) Sets the alignment of the label's text. The alignment parameter can be one of Label.LEFT, Label.CENTER, or Label.RIGHT.
int getAlignment() Retrieves the current alignment of the label's text.
void addNotify() It generates Label's peer.
AccessibleContext getAccessibleContext() It obtains the Accessible Context linked with the label.
protected String paramString() It returns the label's status as a string.

The following classes inherit the methods listed above

java.awt.Component

java.lang.Object

Let us now look at some examples of the labels, in order to get a better understanding of the topic.

Java AWT Label Examples

import javax.swing.*;
import java.awt.*;

public class LabelExample {
    Frame jf;
    Label l1, l2, l3;

    LabelExample() {
        jf = new Frame("Example...");


        l1 = new Label("Label");
        l2 = new Label("Label2");
        l3 = new Label("Label3");


        l1.setText("Welcome to Coding Ninjas!!"); // Setting the text of a JLabal
        jf.setLayout(new FlowLayout());
        jf.add(l1); // Adding label1 to the Jframe
        jf.add(l2); // Adding label2 to the Jframe
        jf.add(l3); // Adding label3 to the Jframe
        jf.setVisible(true);
        jf.setSize(300, 300);
    }


    public static void main(String... ar) {
        new LabelExample();
    }
}

Output

When you execute the above code, you'll see the following window:

Frequently Asked Questions

What is the label in Java?

A Label object is used to place text in a container. A label shows one line of read-only content. The application can update the text, but the user cannot edit it directly.

How to label a statement in Java?

To label a statement in Java, you can use a label followed by a colon (:) before the statement. For example, myLabel: for (int i = 0; i < 5; i++) { // statement }.

What is a label and a button?

In Java, a label is a component used to display text or an image, typically in a graphical user interface. A button is a component that triggers an action when clicked, commonly used for user interaction in GUI applications.

Conclusion

In this article, we have extensively discussed the Java AWT Label. We learned about the properties, constructors, methods of a label, and how to create it.

We hope that this blog has helped you enhance your knowledge of Java and Java AWT and if you would like to learn more about java, check out our other articles. 

Refer to our Guided Path on Naukri Code 360 to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests.

You can also consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass