📜  Java AWT Label(1)

📅  最后修改于: 2023-12-03 14:42:13.173000             🧑  作者: Mango

Java AWT Label

Java AWT (Abstract Windows Toolkit) is a set of classes used for developing GUI (Graphical User Interface) in Java. AWT provides the components like windows, dialogs, buttons, labels, etc.

A label is a GUI component that displays text or an image. AWT Label is an implementation of the label component. It is used to display text, icons, or images on the screen.

The AWT Label class provides several constructors to create a label object. The most commonly used constructors are:

Label()       // creates an empty label with no text
Label(String) // creates a label with the given text

Once the label object is created, you can set its properties using various methods. Some of the commonly used methods are:

setText(String) // sets the text of the label
setAlignment(int) // sets the alignment of the label
setFont(Font) // sets the font of the label
setForeground(Color) // sets the foreground color of the label
setBackground(Color) // sets the background color of the label

Here is an example code snippet that creates a label and sets its properties:

import java.awt.*;

public class LabelExample {
   public static void main(String[] args) {
      Frame f = new Frame("Label Example");
      Label label = new Label("Hello, World!");
      label.setAlignment(Label.CENTER);
      label.setFont(new Font("Serif", Font.BOLD, 36));
      label.setForeground(Color.RED);
      f.add(label);
      f.setSize(400, 400);
      f.setVisible(true);
   }
}

This code creates a new label with the text "Hello, World!" and sets its alignment to center, font to Serif with a bold style and size of 36, foreground color to red, and background color to the default color. It then adds the label to a new frame and sets the size of the frame to 400x400 and makes it visible on the screen.

In conclusion, the Java AWT Label component is used to display text or images on a GUI. It provides various constructors and methods to set its properties.