JLabel Controls in Java Swing

JLabel Controls in Java Swing

It’s used to display one line of read-only text. It is completely different from a textbox.  The text is often changed by an application but a user cannot edit it directly. It inherits the JComponent class.

JLabel Constructors

JLabel(): Creates a JLabel instance with no image and with an empty string for the title.

JLabel(String s): Creates a JLabel instance with the specified text.

JLabel(Icon i): Creates a JLabel instance with the specified image.

JLabel(String s, Icon I, int horizontalAlignment):  creates a JLabel instance with the specified text, image, and horizontal alignment.

Example of JLabel Controls in Java Swing

import javax.swing.*;

import java.awt.*;

class LabelExample 

public static void main(String args[]) 

    { 

    JFrame f= new JFrame(“Pravuram”); 

    JLabel l1,l2; 

     Font ff = new Font (“arial”, Font.BOLD, 23);

        l1=new JLabel(“Classes”); 

    l1.setFont (ff);

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

    l2=new JLabel(“JAVA”);

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

    f.add(l1);

    f.add(l2); 

    f.setSize(300,300); 

    f.setLayout(null); 

    f.setVisible(true); 

    } 

    } 

Leave a Comment

Scroll to Top