jlabel in java

In Java, JLabel is a Swing component used to display text, image, or both. It is a subclass of JComponent and is typically used to provide a descriptive caption for other components.

Here is an example of how to use JLabel to display text:

refe‮t r‬o:theitroad.com
import javax.swing.*;

public class JLabelExample {
    public static void main(String[] args) {
        // Create a new JFrame
        JFrame frame = new JFrame("JLabel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Create a new JLabel with some text
        JLabel label = new JLabel("Hello, world!");
        
        // Add the JLabel to the JFrame
        frame.getContentPane().add(label);
        
        // Set the size and make the frame visible
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

In this example, a JLabel is created with the text "Hello, world!" and added to a JFrame. The frame is then sized and made visible. When you run the program, you will see a window with the text "Hello, world!" displayed in the center.

JLabel has a number of properties that can be set to control its appearance, including font, foreground color, and background color. Additionally, JLabel can be used to display images by setting its icon property to an instance of the Icon interface.