JLabel | Java Swing


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 :

  1. JLabel() : creates a blank label with no text or image in it.
  2. JLabel(String s) : creates a new label with the string specified.
  3. JLabel(Icon i) : creates a new label with a image on it.
  4. 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 :



  1. getIcon() : returns the image that that the label displays
  2. setIcon(Icon i) : sets the icon that the label will display to image i
  3. getText() : returns the text that the label will display
  4. 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output :

2. Program to create a new label using constructor – JLabel(String s)

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output :

3. Program to create a label and add image to it .

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output :

4. Program to add a image and string to a label

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to add a image and string 
// to a label with horizontal alignment
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 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();
    }
}

chevron_right


Output :

Note : This programs might not run in an online compiler please use an offline IDE.



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet, Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.