In this article AWT Label Component  we give the information about AWT Label is a simple component which is used to display a static text. It can be used with any other component like button, textfield etc. to give information to the user about the function of a component.

AWT Label Component:

Label Component:

Label in AWT (Abstract Window Toolkit) is a GUI (Graphical User Interface) component which is used to display a text. It is a simple non-editable text which we can only view but cannot modify.

AWT Label is a simple component which is used to display a static text. It can be used with any other component like button, textfield etc. to give information to the user about the function of a component.

Attributes of label in AWT:

Label in AWT (Abstract Window Toolkit) has many attributes, which we can use to control its appearance and behavior. Below are the main attribute:

1. Text

Description: This is the text that is displayed in the label.

Method:

getText(): Gets the current text of the label.

setText(String text): Sets or changes the text displayed in the label.

Example:

Label label = new Label(“Hello, World!”);

label.setText(“Welcome!”);  // Change the label text

2. Alignment

Description: Determines how the text inside the label will be aligned. Text can be aligned left, center, or right.

Values:

Label.LEFT – Aligns the text left.

Label.CENTER – Aligns the text center.

Label.RIGHT – Aligns the text right.

Method:

getAlignment(): Gets the current text alignment.

setAlignment(int alignment): Sets the alignment of the text.

Example:

label.setAlignment(Label.CENTER);  // Set the label text alignment to center

3. Font

Description: Determines what type of font (typeface, size, and style) will be used for the label’s text.

Method:

setFont(Font f): Sets the label’s font.

Example:

Font font = new Font(“Serif”, Font.BOLD, 14);

label.setFont(font);  // Set a bold font with size 14

4. Foreground Color

Description: Sets the color of the text displayed in the label.

Method:

setForeground(Color c): Sets the color of the label’s text.

Example:

label.setForeground(Color.RED);  // Set the text color to red

5. Background Color

Description: Sets the background color of the label.

Method: setBackground(Color c): Sets the background color of the label.

Example:

label.setBackground(Color.YELLOW);  // Set the background color to yellow

6. Enabled/Disabled

Description: Determines whether the label is enabled or disabled. However, labels are usually enabled.

Method: setEnabled(boolean status): Enables or disables the label.

Example:

label.setEnabled(false);  // Disable the label

7. Size

Description: Sets the width and height of the label.

Method:

getSize(): Gets the current size of the label.

setSize(int width, int height): Sets the size of the label.

Example:

label.setSize(150, 50);  // Set the label size to 150×50

8. Bounds

Description: Sets the position and size of a label within its container.

Method:

setBounds(int x, int y, int width, int height)

Sets the position (x, y) and size (width, height) of the label.

Example:

label.setBounds(50, 50, 200, 30);  // Set position (50,50) and size (200×30)

9. Visible

Description: Determines whether the label will be visible on the screen.

Method:

setVisible(boolean visible): Makes the label visible or invisible.

Example:

label.setVisible(false);  // Hide the label

Example:

import java.awt.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class LabelExample

{

            public static void main(String args[])

            {

            // Create Frame

            final Frame f = new Frame(“Label Example with attributes”);

            // Create Label

            Label l1 = new Label(“Wel-Come!”);

            l1.setBounds(50,100,200,30);

                        Font font = new Font(“Serif”, Font.BOLD, 14);

                        l1.setFont(font);

                        l1.setAlignment(l1.CENTER);

                        l1.setForeground(Color.RED);

                        l1.setBackground(Color.YELLOW);

                        l1.setSize(150,50);

                        l1.setBackground(Color.YELLOW);

                        l1.setText(“Hello, World!”);  // Change the label text

                        // frame attributes

                        f.add(l1);

            f.setSize(300,300);

            f.setLayout(null);

            f.setVisible(true);

            f.addWindowListener(new WindowAdapter()

                        {

                                    public void windowClosing(WindowEvent e)

                                    {

                                    f.dispose();

                                    }

                }  );

            }

}

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *