INTRODUCTION OF JLABEL

Introduction of JLabel


JLabels

    Java Swing Tutorial Explaining the JLabel Component.A JLabel object provides text instructions or information on a GUI — display a single line of read-only text, an image or both text and image.

    JLabels

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

    JLabel(Icon image)

    Creates a JLabel instance with the specified image.

    JLabel(Icon image, int horizontalAlignment)

    Creates a JLabel instance with the specified image and horizontal alignment.

    JLabel(String text)

    Creates a JLabel instance with the specified text.

    JLabel(String text, int horizontalAlignment)

    Creates a JLabel instance with the specified text and horizontal alignment.

Example:JLabel Source Code

 
            
                package com.swing.sdj;
                import java.awt.GridLayout;
                import javax.swing.ImageIcon;
                import javax.swing.JFrame;
                import javax.swing.JLabel;
                public class SdjLebelDemo  extends JFrame
                {
                    public static void main(String[] args)
                    {
                        SdjLebelDemo levelDemo=new SdjLebelDemo();
                        levelDemo.setVisible(true);
                        levelDemo.setSize(500, 500);
                        levelDemo.setTitle("SDJ Lebel Demo");
                        levelDemo.setDefaultCloseOperation(SdjLebelDemo.EXIT_ON_CLOSE);
                    }
                    JLabel jlbl1,jlbl2,jlbl3;
                    public  SdjLebelDemo()
                    {
                        setLayout(new GridLayout(3,1));
                        ImageIcon icon=new ImageIcon("D:\sdj.jpg","My SDJ");
                        jlbl1=new JLabel("SDJ Image with Text",icon,JLabel.CENTER);
                        jlbl1.setVerticalTextPosition(JLabel.BOTTOM);
                        jlbl1.setHorizontalTextPosition(JLabel.CENTER);
                        jlbl2=new JLabel("SDJ label for only text");
                        jlbl3=new JLabel(icon);
                        add(jlbl1);
                        add(jlbl2);
                        add(jlbl3);
                    }
                } 
            

Output:

Naming Convention
SDJ Label Demo