Java Swing JLabel Example

By | 2017-10-30T03:48:00+00:00 October 30th, 2017|java|0 Comments

Here we are going to work with the most useful component in Java Swing JLabel.

Swing JLabel :

We can display a small text using JLabel component in Swing.

Simple JLabel Demo :


package com.swing.examples;

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class LabelDemo extends JFrame {

    JLabel label;
    Container container;

    LabelDemo() {
        super("My JLabel Demo");
        label = new JLabel("Hello World!");
        container = getContentPane();
        container.add(label);
        setSize(200, 200);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new LabelDemo();
    }
}

JLabel with html content :

We can apply the html elements while using JLabel component.


package com.swing.examples;

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class LabelDemo extends JFrame {

    JLabel label;
    Container container;

    LabelDemo() {
        super("My JLabel Demo");
        label = new JLabel("<html><font color='red'>Hello World</font></html>");
        container = getContentPane();
        container.add(label);
        setSize(200, 200);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new LabelDemo();
    }
} 

 

JLabel with images :

We can use the images as JLabel.


package com.swing.examples;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class LabelDemo extends JFrame {

    JLabel label;
    ImageIcon icon;

    Container container;

    LabelDemo() {
        super("My JLabel Demo");
        icon = new ImageIcon("C:\\images\\dukeWave.gif"); // Your Images path
        label = new JLabel("Hello World !", icon, SwingConstants.LEFT);

        container = getContentPane(); // new ContentPane();

        container.setLayout(new FlowLayout());

        container.add(label);

        setSize(300, 200);

        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    public static void main(String[] args) {
        new LabelDemo();
    }
}


Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment