Abstract Window Toolkit (AWT) was Java's first GUI framework, and it has been part of Java since version 1.0. It contains numerous classes and methods that allow you to create windows and simple controls. Controls are components that allow a user to interact with your application in various ways.
The AWT supports the following types of controls:
- Labels
- Push buttons
- Checkboxes
- Choice lists
- Lists
- Scroll bars
- Text Editing
The easiest control to use is a label. A Label is an object of type Label (class in java.awt.Component), and it contains a string, which it displays within a container. Labels are passive controls that do not support any interaction with the user. To create a label, we need to create the object of the Label class.
What is Label in Java AWT?
Label in Java AWT is a component for placing text or images in a container. The text displayed in the Container (Display) by Java AWT Label can't be edited directly by the user but needs the programmer to change it from his side.
Note: Java AWT Label is also called passive control as no event is created when it is accessed.
Syntax of Java AWT Label
public class Label extends Component implements Accessible
Java AWT Label Class Constructors
There are three types of Java AWT Label Class
1. Label():
Creates Empty Label.
2. Label(String str):
Constructs a Label with str as its name.
3. Label(String str, int x):
Constructs a label with the specified string and x as the specified alignment
Java AWT Label Fields
Java AWT Labels have 3 fields there can also be determined as the java.awt.Component class fields. All of them are mentioned below:
|
static int LEFT
| It specifies that the label is aligned left.
|
static int RIGHT
| It specifies that the label is aligned right.
|
static int CENTER
| It specifies that the label is aligned center.
|
Java AWT Label Class Methods
There are a few predefined Methods associated with Java AWT Label as mentioned below:
|
1.
| void setText(String text) | Sets the text of the label. |
2.
| String getText() | Gets the text of the label. |
3.
| void setAlignment(int alignment) | Sets the alignment of the text in the label. Possible values: Label.LEFT , Label.RIGHT , Label.CENTER . |
4.
| int getAlignment() | Gets the alignment of the text in the label. |
5.
| void addNotify() | Creates the peer for the label. |
6.
| AccessibleContext getAccessibleContext() | Gets the AccessibleContext associated with the label. |
7.
| void addNotify()
| Creates the peer (native window) for the label.
|
Methods inherited
The Methods provided with AWT Label is inherited by classes mentioned below:
- java.awt.Component
- java.lang.Object
Java AWT Label Examples
There are certain cases associated with Java AWT Label so let us explore them as examples below:
Example 1: ( Basic Printing Hello World! )
Java
// Java AWT Program to
// demonstrate Use of Label
// Hello World Program in AWT
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// Driver Class
class Main {
// Main Function
public static void main(String[] args)
{
// Declaring Frame
Frame frame = new Frame("Basic");
// All the components will be
// inserted in this frame
// Declaring Label
Label label = new Label("Hello World!");
// Adding Label into Frame
frame.add(label);
// Setting Frame Size
frame.setSize(500, 500);
// Setting Visibility of Frame
frame.setVisible(true);
// Using WindowListener for closing the window
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
Compiling and Executing the Program
javac Main.java
java Main
Output:
-300.webp)
Example 2: (Printing Center Aligned Text)
In this example we will check how to change the alignment of the Label we are putting into the frame.
Below is the implementation of the Example:
Java
// Java AWT Program to
// demonstrate Use of
// setAlignment
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// Driver Class
class Main {
// Main Function
public static void main(String[] args)
{
// Declaring Frame
Frame frame = new Frame("Basic");
// All the components will be
// inserted in this frame
// Declaring Label
Label label = new Label("Change is Here");
// Setting the Aligment to CENTER
label.setAlignment(Label.CENTER);
// Adding Label into Frame
frame.add(label);
// Setting Frame Size
frame.setSize(500, 500);
// Setting Visibility of Frame
frame.setVisible(true);
// Using WindowListener for closing the window
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
Compiling and Executing the Program
javac Example2.java
java Example2
Output:
-(1)-300.webp)
Example 3: ( ActionListener )
In this example , we are creating objects of Label and Button classes which are added to the Frame. Using actionPerformed() method an event is generated over the button. When we click the button we will get the result if a number is odd or even.
Below is the implementation of the above method:
Java
// Java AWT Label Program to Check
// if a number is Odd or event
// Using ActionListener
import java.awt.*;
import java.awt.event.*;
// Driver Class
public class Example3
extends Frame implements ActionListener {
// Frame Label and Button Initialised
private Frame frame;
private Label label;
private Button button;
public int count = 0;
// Constructor
public Example3()
{
// Memory Allocated to frame
frame = new Frame("Example 3 of AWT Label");
// Memory Allocated to label
label
= new Label("Check if a Number is Even or Odd");
// Memory Allocated to button
button = new Button("Calculate");
// Register ActionListener
button.addActionListener(this);
// Add components to the frame
frame.add(label, BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);
// Dimensions of Frame
frame.setSize(300, 300);
frame.setVisible(true);
// Using WindowListener for closing the window
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
// Action Listener checks when
// Button is Pressed
@Override public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button) {
String str;
if (count % 2 == 0)
str = count + " is Even";
else
str = count + " is odd";
label.setText(str);
count++;
}
}
// main function
public static void main(String[] args)
{
Example3 example = new Example3();
}
}
Compiling and Executing the Program
javac Example3.java
java Example3
Output Before Any Click:

Output After First Click:

Output After Second Click:

Conclusion
We hope you learned something new from this article on the Java AWT Label class. As a Java developer, you'll find this Label class to be a very useful component. It is simple to use, but it can be customized to meet the needs of any application. Experiment with the Label class and see how you can use it to make your user interfaces more informative and engaging.
Similar Reads
Java AWT Panel
In Java's Abstract Window Toolkit (AWT), the Panel class is a fundamental component for creating graphical user interfaces. It offers a straightforward way to organize and group various GUI elements. This article explores the Panel class in Java AWT, covering its essential aspects, methods, and cons
2 min read
JavaFX | Label
Label is a part of JavaFX package . Label is used to display a short text or an image, it is a non-editable text control. It is useful for displaying text that is required to fit within a specific space, and thus may need to use an ellipsis or truncation to size the string to fit. Labels also are us
6 min read
Java AWT Tutorial
Java AWT or Abstract Window Toolkit is an API used for developing GUI(Graphic User Interfaces) or Window-Based Applications in Java. Java AWT is part of the Java Foundation Classes (JFC) that provides a way to build platform-independent graphical applications. In this AWT tutorial, you will learn th
13 min read
Java AWT TextField
Java Abstract Window Toolkit (AWT) is a library for building interactive Graphical User Interfaces (GUIs) in Java Applications. In AWT, TextField is a text component that lets users add a single line of text and edit it further. TextField in Java AWTThe TextField class in Java AWT is a GUI component
4 min read
Java AWT Toolkit
The Abstract Window Toolkit (AWT) is a Java package that provides a platform-indepеndеnt sеt of tools for creating graphical usеr intеrfacеs (GUIs). AWT is part of thе Java Foundation Classеs (JFC), which also includes Swing for morе advancеd GUI dеvеlopmеnt. In this rеsponsе, I'll providе an ovеrvi
6 min read
JLabel | Java Swing
JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus. By default labels are ver
4 min read
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1991. It follows the Object-oriented Programming concept and can run programs written on any OS platform. Java is a high-level, object-oriented, secure, robust, platform-independent, multi
15+ min read
JavaFX | Tab Class
Tab class is a part of JavaFX. Tab class creates an object that is contained in TabPane. A Tab can contain any node as its content. A TabPane can contain multiple tabs. When the user clicks on the tab the content of the tab is made visible to the user. Constructors of the class: Tab(): Creates a new
4 min read
Java KeyListener in AWT
The Java KeyListener in the Abstract Window Toolkit (AWT) is a fundamental tool for achieving this. The KeyListener Interface is found in "java.awt.event" package. In this article, we'll explore what the KeyListener is, and its declaration methods, and supply examples with explanatory comments. Java
3 min read
JavaFX | TabPane Class
TabPane class is a part of JavaFX. TabPane allows switching between several tabs. TabPane acts as a container of tabs. The positions of the tabs can be specified using the setSide() function. when the tabs do not fit in the TabPane, a menu button appears at the upper right corner of TabPane which di
5 min read