How to add label in AWT program in java

import java.awt.*;

class LabelExample
{
    public static void main(String args[])
    {
        Frame f = new Frame("Label Example by TechSarvam");
        Label l1, l2;
        
        l1 = new Label("First Label.");
        l1.setBounds(50, 100, 100, 30);
        
        l2 = new Label("Second Label.");
        l2.setBounds(50, 150, 100, 30);
        
        f.add(l1);
        f.add(l2);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
    }
}

This is a Java program that shows how to use Label components in a simple GUI using the AWT (Abstract Window Toolkit) library. The program creates a frame and adds two labels to it. Let’s break down the program step by step.

1. Importing AWT Package

import java.awt.*;

This imports the AWT package, which provides the necessary classes for creating GUI components such as Frame, Label, etc.

2. Class Definition

class LabelExample {

This declares the LabelExample class with the main method that will execute the program.

3. Main Method

public static void main(String args[]) {

The main method is where the program starts. It will be called whenever the program is run.

4. Frame Declaration

Frame f = new Frame(“Label Example by TechSarvam”);

Here, a Frame object f is declared. The frame is a top-level window with a title. The title of the frame is  “Label Example by TechSarvam”, which will appear at the top of the window.

5. Creating Label Objects

Label l1, l2;

l1 = new Label(“First Label.”);

l2 = new Label(“Second Label.”);

Two Label objects are declared:

  • l1 with the text  “First Label.”
  • l2 with the text  “Second Label.”

A Label is simple component for putting an ordinary small length string within a window:

6. Setting Location and Size to the Labels

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

l2.setBounds(50, 150, 100, 30);

The ‘setBounds() is used above is to establish where the window in which all such components placed relative to this Label’s will position in along its X as well as the Y-axis coordinates for positioning (of this JLabel also) it should have is based on that.

  • 100: The width of the label.
  • 30: The height of the label.
  •  l1 will be displayed at (50, 100) with 100×30 pixels size
  •  l2 will be displayed at (50, 150) with 100×30 pixels size

7. Adding Labels to Frame

f.add(l1);

f.add(l2);

The labels l1 and l2 will be added inside the frame f such that they would be displayed

8. Setting Frame Properties

f.setSize(400, 400);

f.setLayout(null);

f.setVisible(true);

  • f.setSize(400, 400);: Sets frame size to 400 pixels wide and 400 pixels tall.
  • f.setLayout(null);: Turns off the layout manager for the frame; thus, components must be placed in absolute position using setBounds().
  • f.setVisible(true);: Set frame to visible. If this line of code is not used then frame will never appear on screen.

9. Program Output

When you run the program a window (frame) should appear with all of the following features:

  • A window with the title  “Label Example by TechSarvam”.
  • The window size will be 400×400 pixels.

Inside the window, there will be two labels:

  • The first label (“First Label.”) at the position (50, 100).
  • The second label (“Second Label.”) at the position (50, 150).

Example Output (Graphical UI):

A 400×400 window.

Two labels inside the window:

  • “First Label.” at the coordinates (50, 100).
  • “Second Label.” at the coordinates (50, 150).

Since the program uses setLayout(null), the labels are manually placed using setBounds() and won’t be affected by any layout manager.

Conclusion:

This program demonstrates how to implement a simple framed application displaying labels in the Java AWT environment. The program does the following:

  • It declares a Frame object with a name and assigns the name “Java Frame.”
  • Declares two Label objects and assigns text to both.
  • Uses setBounds() to size and position the labels
  • Adds the two labels to the frame
  • Makes the frame visible at a predefined size

The window will come out with the two labels appropriately positioned.

Leave a Comment

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