import javax.swing.*; public class MyWindow extends JFrame { public MyWindow() { super("My Window"); JLabel label = new JLabel("Hello World"); add(label); pack(); setVisible(true); } public static void main(String[] args) { new MyWindow(); } }
import javax.swing.*; import java.awt.*; public class MyWindow extends JFrame { public MyWindow() { super("My Window"); ImageIcon icon = new ImageIcon("path/to/image.jpg"); JLabel label = new JLabel(icon); add(label); pack(); setVisible(true); } public static void main(String[] args) { new MyWindow(); } }In this example, an ImageIcon is created with an image file and used to create a JLabel. The pack() and setVisible(true) methods are the same as in Example 1. The package library for javax.swing.JLabel is javax.swing.