Java Swing Program to demonstrate JLabel with Text and Icon

Program

import javax.swing.*;
public class JLabelTextIconDemo
{
	public static void main(String args[])
	{
		JFrame frm = new JFrame("JLabel Demo");
		JLabel lblUserName, lblPassword;
		ImageIcon icoUserName, icoPassword;
		icoUserName = new ImageIcon("username.png");
		lblUserName = new JLabel();
		lblUserName.setText("User Name");
		lblUserName.setIcon(icoUserName);
		lblUserName.setBounds(0, 0, 150, 50);
		icoPassword = new ImageIcon("password.png");
		lblPassword = new JLabel();
		lblPassword.setText("Password");
		lblPassword.setIcon(icoPassword);
		lblPassword.setBounds(0, 50, 150, 50);
		frm.add(lblUserName);
		frm.add(lblPassword);
		frm.setSize(300, 300); 
		frm.setLayout(null);
		frm.setVisible(true);
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

Output

Swing JLabel with text and icon

Tags: