JLabel with ImageIcon

The swing icons are encapsulated by ImageIcon class. This class paints an image from the image.

ImageIcon(String filename)

Program

1 - Copy

 

 

 

Program Source

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.FlowLayout;

class Frame extends JFrame{
    
    Frame()
    {
        setTitle("ImageIcon");
        setLayout(new FlowLayout());
        setLabel();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void setLabel()
    {
        ImageIcon icon = new ImageIcon("india.png");
        JLabel jl = new JLabel("Indian Flag",icon,JLabel.CENTER);
        add(jl);
    }
}

public class Javaapp {
   
    public static void main(String[] args) {
        
        Frame frame = new Frame();
    }
}

Leave a Comment