In Java AWT (Abstract Window Toolkit), Label
is a class that represents a non-editable text component that displays a single line of read-only text to the user.
The Label
class is a subclass of the Component
class and can be used to display a short string or an image in a graphical user interface (GUI). It provides various methods to manipulate the text and appearance of the label.
Here’s an example of how to create a Label
and add it to a GUI using Java AWT:
import java.awt.*; public class LabelExample { public static void main(String[] args) { Frame frame = new Frame("Label Example"); Label label = new Label("Hello World!"); frame.add(label); frame.setSize(300, 300); frame.setVisible(true); } }
In this example, a Frame
object is created with the title “Label Example”. A Label
object is also created with the text “Hello World!” and added to the frame using the add()
method. Finally, the size of the frame is set to 300×300 pixels and it is made visible to the user.
You can also change the appearance of the label using methods such as setFont()
and setForeground()
. Additionally, you can use the setIcon()
method to display an image instead of text.
AWT Label Class Declaration:
The declaration of the Label
class in Java AWT is as follows:
public class Label extends Component implements Accessible
Here, Label
is a subclass of the Component
class and also implements the Accessible
interface. As a subclass of Component
, Label
inherits various methods and properties from its parent class that allow it to be displayed on a graphical user interface (GUI).
The Accessible
interface is used to provide accessibility support for disabled users who rely on assistive technologies to interact with the GUI. By implementing this interface, Label
provides additional accessibility features, such as support for screen readers.
In addition to the methods and properties inherited from Component
, the Label
class also provides its own methods and properties that can be used to manipulate the appearance and behavior of the label component. Some of the commonly used methods of the Label
class include setText()
, setFont()
, setForeground()
, setBackground()
, and setAlignment()
.
AWT Label Fields:
The Label
class in Java AWT defines several fields that can be used to configure and manipulate the appearance and behavior of a label component. Some of the commonly used fields of the Label
class include:
LEFT
: This field is used to specify the alignment of the label text to the left side of the component.CENTER
: This field is used to specify the alignment of the label text to the center of the component.RIGHT
: This field is used to specify the alignment of the label text to the right side of the component.LEADING
: This field is used to specify the alignment of the label text to the leading edge of the component, depending on the user’s language direction.TRAILING
: This field is used to specify the alignment of the label text to the trailing edge of the component, depending on the user’s language direction.text
: This field is used to store the text displayed by the label.alignment
: This field is used to store the current alignment of the label text.font
: This field is used to store the current font used for the label text.foreground
: This field is used to store the current color used for the label text.background
: This field is used to store the current color used for the label background.
These fields can be accessed and modified using the getter and setter methods provided by the Label
class. For example, the getText()
method can be used to retrieve the current text displayed by the label, while the setText()
method can be used to set a new text for the label. Similarly, the getAlignment()
method can be used to retrieve the current alignment of the label text, while the setAlignment()
method can be used to set a new alignment.
Label class Constructors:
The Label
class in Java AWT provides several constructors to create a new label object. The available constructors are:
Label()
: Creates a new label with no text.Label(String text)
: Creates a new label with the specified text.Label(String text, int alignment)
: Creates a new label with the specified text and alignment. The alignment can be one of the constantsLabel.LEFT
,Label.CENTER
,Label.RIGHT
,Label.LEADING
, orLabel.TRAILING
.Label(String text, int alignment, boolean isVertical)
: Creates a new label with the specified text, alignment, and orientation. IfisVertical
istrue
, the label is displayed vertically, otherwise it is displayed horizontally. This constructor is only available in Java 9 and later versions.
Each constructor creates a new label object with the specified properties. For example, the following code creates a new label with the text “Hello, World!” and sets its alignment to Label.CENTER
:
Label label = new Label("Hello, World!", Label.CENTER);
You can also set the text and alignment of an existing label object using the setText()
and setAlignment()
methods. For example:
label.setText("New text"); label.setAlignment(Label.LEFT);
Note that the default alignment for a label is Label.CENTER
, and the default orientation is horizontal.
Label Class Methods:
The Label
class in Java AWT provides several methods to manipulate the appearance and behavior of a label component. Some of the commonly used methods of the Label
class are:
setText(String text)
: Sets the text displayed by the label to the specified string.setAlignment(int alignment)
: Sets the alignment of the label text to the specified value. The alignment can be one of the constantsLabel.LEFT
,Label.CENTER
,Label.RIGHT
,Label.LEADING
, orLabel.TRAILING
.setFont(Font font)
: Sets the font used for the label text to the specified font.setForeground(Color color)
: Sets the color used for the label text to the specified color.setBackground(Color color)
: Sets the color used for the label background to the specified color.getPreferredSize()
: Returns the preferred size of the label based on its current contents and font.getMinimumSize()
: Returns the minimum size of the label based on its current contents and font.getPreferredSize(int width)
: Returns the preferred size of the label based on the specified width and the label’s current font and contents.getMinimumSize(int width)
: Returns the minimum size of the label based on the specified width and the label’s current font and contents.getImage()
: Returns the image displayed by the label, if any.setImage(Image image)
: Sets the image to be displayed by the label to the specified image.
These methods can be used to customize the appearance and behavior of a label component in a graphical user interface (GUI). For example, the setText()
method can be used to change the text displayed by the label, while the setForeground()
method can be used to change the color of the label text.
Method inherited:
As with all Java classes, the Label
class inherits several methods from its superclasses and interfaces, such as the Component
class and the Serializable
interface. Some of the commonly used methods inherited by the Label
class include:
setVisible(boolean visible)
: Sets the visibility of the label to the specified value.setEnabled(boolean enabled)
: Sets the enabled state of the label to the specified value.getLocation()
: Returns the current location of the label on the screen.setLocation(int x, int y)
: Sets the location of the label on the screen to the specified coordinates.getSize()
: Returns the current size of the label.setSize(int width, int height)
: Sets the size of the label to the specified dimensions.addMouseListener(MouseListener listener)
: Adds a mouse listener to the label to receive mouse events.removeMouseListener(MouseListener listener)
: Removes the specified mouse listener from the label.getAccessibleContext()
: Returns the accessible context for the label, which allows assistive technologies to interact with the component.
These inherited methods can be used to perform common GUI operations on the label component, such as changing its visibility or size, adding mouse listeners, and providing accessibility information.
Java AWT Label Example:
Here’s an example of how to use the Label
class in Java AWT to create a simple GUI application that displays a label with text:
import java.awt.*; public class LabelExample { public static void main(String[] args) { Frame frame = new Frame("Label Example"); Label label = new Label("Hello, World!", Label.CENTER); frame.add(label); frame.setSize(300, 200); frame.setVisible(true); } }
In this example, we create a new Frame
object and set its title to “Label Example”. We then create a new Label
object with the text “Hello, World!” and set its alignment to Label.CENTER
. We add the label to the frame using the add()
method, and set the size of the frame to 300×200 pixels. Finally, we make the frame visible using the setVisible()
method.
When you run this program, you will see a window containing a label with the text “Hello, World!” displayed in the center of the window.
You can customize the appearance and behavior of the label component using the various methods of the Label
class, such as setText()
, setAlignment()
, setFont()
, and setForeground()
. You can also add event listeners to the label component to handle mouse and keyboard events.
Java AWT Label Example with ActionListener:
Here’s an example of how to use the Label
class in Java AWT with an ActionListener
to create a GUI application that displays a label with changing text:
import java.awt.*; import java.awt.event.*; public class LabelExample implements ActionListener { private Label label; private int counter = 0; public static void main(String[] args) { new LabelExample(); } public LabelExample() { Frame frame = new Frame("Label Example"); label = new Label("Counter: 0", Label.CENTER); Button button = new Button("Click me!"); button.addActionListener(this); Panel panel = new Panel(new GridLayout(2, 1)); panel.add(label); panel.add(button); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { counter++; label.setText("Counter: " + counter); } }
In this example, we create a new Frame
object and set its title to “Label Example”. We also create a new Label
object with the initial text “Counter: 0” and set its alignment to Label.CENTER
.
We then create a new Button
object with the text “Click me!” and add an ActionListener
to it using the addActionListener()
method. The actionPerformed()
method of the LabelExample
class is called whenever the button is clicked, and increments the counter
variable and updates the text of the label to display the new counter value.
We create a new Panel
object with a GridLayout
of 2 rows and 1 column, and add the label and button components to it. We then add the panel to the frame using the add()
method, set the size of the frame to 300×200 pixels, and make the frame visible using the setVisible()
method.
When you run this program, you will see a window containing a label with the text “Counter: 0” and a button with the text “Click me!”. Every time you click the button, the counter value will be incremented and the label text will be updated to display the new counter value.