AWT Label Class

Advertisements


Introduction

Label is a passive control because it does not create any event when accessed by the user. The label control is an object of Label. A label displays a single line of read-only text. However the text can be changed by the application programmer but cannot be changed by the end user in any way.

Class declaration

Following is the declaration for java.awt.Label class:

public class Label
   extends Component
      implements Accessible

Field

Following are the fields for java.awt.Component class:

  • static int CENTER -- Indicates that the label should be centered.

  • static int LEFT -- Indicates that the label should be left justified.

  • static int RIGHT -- Indicates that the label should be right justified.

Class constructors

S.N.Constructor & Description
1Label()
Constructs an empty label.
2Label(String text)
Constructs a new label with the specified string of text, left justified.
3Label(String text, int alignment)
Constructs a new label that presents the specified string of text with the specified alignment.

Class methods

S.N.Method & Description
1void addNotify()
Creates the peer for this label.
2AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Label.
3int getAlignment()
Gets the current alignment of this label.
4String getText()
Gets the text of this label.
5protected String paramString()
Returns a string representing the state of this Label.
6void setAlignment(int alignment)
Sets the alignment for this label to the specified alignment.
7void setText(String text)
Sets the text for this label to the specified text.

Methods inherited

This class inherits methods from the following classes:

  • java.awt.Component

  • java.lang.Object

Label Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >

AwtControlDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showLabelDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showLabelDemo(){
      headerLabel.setText("Control in action: Label");      

      Label label = new Label();
      label.setText("Welcome to TutorialsPoint AWT Tutorial.");
      label.setAlignment(Label.CENTER);
      label.setBackground(Color.GRAY);
      label.setForeground(Color.WHITE);
      controlPanel.add(label);
   
      mainFrame.setVisible(true);  
   }
}

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\AWT>java com.tutorialspoint.gui.AwtControlDemo

Verify the following output

AWT Label

Advertisements
Advertisements