JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus. By default labels are vertically centered but the user can change the alignment of label.
Constructor of the class are :Â
Â
- JLabel() : creates a blank label with no text or image in it.
- JLabel(String s) : creates a new label with the string specified.
- JLabel(Icon i) : creates a new label with a image on it.
- JLabel(String s, Icon i, int align) : creates a new label with a string, an image and a specified horizontal alignment
Commonly used methods of the class are :Â
Â
- getIcon() : returns the image that  the label displays
- setIcon(Icon i) : sets the icon that the label will display to image i
- getText() : returns the text that the label will display
- setText(String s) : sets the text that the label will display to string s
1. Program to create a blank label and add text to it.Â
Â
Java
// Java Program to create a// blank label and add text to it.import java.awt.event.*;import java.awt.*;import javax.swing.*;class text extends JFrame {Â
    // frame    static JFrame f;Â
    // label to display text    static JLabel l;Â
    // default constructor    text()    {    }Â
    // main class    public static void main(String[] args)    {        // create a new frame to store text field and button        f = new JFrame("label");Â
        // create a label to display text        l = new JLabel();Â
        // add text to label        l.setText("label text");Â
        // create a panel        JPanel p = new JPanel();Â
        // add label to panel        p.add(l);Â
        // add panel to frame        f.add(p);Â
        // set the size of frame        f.setSize(300, 300);Â
        f.show();    }} |
Output :Â
Â
2. Program to create a new label using constructor – JLabel(String s)Â
Â
Java
// Java Program to create a new label// using constructor - JLabel(String s)import java.awt.event.*;import java.awt.*;import javax.swing.*;class text extends JFrame {Â
    // frame    static JFrame f;Â
    // label to display text    static JLabel l;Â
    // default constructor    text()    {    }Â
    // main class    public static void main(String[] args)    {        // create a new frame to store text field and button        f = new JFrame("label");Â
        // create a label to display text        l = new JLabel("new text ");Â
        // create a panel        JPanel p = new JPanel();Â
        // add label to panel        p.add(l);Â
        // add panel to frame        f.add(p);Â
        // set the size of frame        f.setSize(300, 300);Â
        f.show();    }} |
Output :Â
Â
3. Program to create a label and add image to it .
Â
Java
// Java Program to create a label// and add image to it .import java.awt.event.*;import java.awt.*;import javax.swing.*;class text extends JFrame {Â
    // frame    static JFrame f;Â
    // label to display text    static JLabel l;Â
    // default constructor    text()    {    }Â
    // main class    public static void main(String[] args)    {        // create a new frame to store text field and button        f = new JFrame("label");Â
        // create a new image icon        ImageIcon i = new ImageIcon("f:/image.png");Â
        // create a label to display image        l = new JLabel(i);Â
        // create a panel        JPanel p = new JPanel();Â
        // add label to panel        p.add(l);Â
        // add panel to frame        f.add(p);Â
        // set the size of frame        f.setSize(500, 500);Â
        f.show();    }} |
Output :
Â
4. Program to add a image and string to a labelÂ
Â
Java
// Java Program to add a image and string// to a label with horizontal alignmentimport java.awt.event.*;import java.awt.*;import javax.swing.*;class text extends JFrame {Â
    // frame    static JFrame f;Â
    // label to display text    static JLabel l;Â
    // default constructor    text()    {    }Â
    // main class    public static void main(String[] args)    {        // create a new frame to store text field and button        f = new JFrame("label");Â
        // create a new image icon        ImageIcon i = new ImageIcon("f:/image.png");Â
        // create a label to display text and image        l = new JLabel("new image text ", i, SwingConstants.HORIZONTAL);Â
        // create a panel        JPanel p = new JPanel();Â
        // add label to panel        p.add(l);Â
        // add panel to frame        f.add(p);Â
        // set the size of frame        f.setSize(600, 500);Â
        f.show();    }} |
Output :Â
Â
Note : This programs might not run in an online compiler please use an offline IDE.
Â

