JLabel in Java

JLabel is a class in Java's Swing GUI toolkit that represents a display area for a short text string or an image, or both. It is often used to display static text or images that don't require user interaction. JLabels can be added to any container that uses a layout manager, such as JFrame, JPanel, or JDialog.

JLabel provides several constructors that allow you to create a label with various properties, such as text, icon, alignment, and border. You can set or get the properties of a label using its methods, such as setText(), setIcon(), setHorizontalAlignment(), and setBorder().

Here is an example of creating a label with text and adding it to a JFrame:

scss

import javax.swing.*; 
 public class MyFrame extends JFrame 
{ public MyFrame() 

 JLabel label = new JLabel("Hello, World!"); 
add(label); 
setTitle("My Frame"); 
setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); 
 } 
 public static void main(String[] args) { 
 new MyFrame(); 
 } 
}

This code creates a JFrame with a JLabel that displays the text "Hello, World!". The label is added to the frame using the add() method, and the frame is displayed using the setVisible() method.

Comments

Popular posts from this blog

Cryptography API

Java Applet Overview

Vector in Java