A tour of swing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JlabelDemo extends JPanel {
JLabel jlbLabel1, jlbLabel2, jlbLabel3;
public JlabelDemo() {
ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG",
"My Website");
setLayout(new GridLayout(3, 1));
jlbLabel1 = new JLabel("Image with Text", icon, JLabel.CENTER);
jlbLabel1.setVerticalTextPosition(JLabel.BOTTOM);
jlbLabel1.setHorizontalTextPosition(JLabel.CENTER);
jlbLabel2 = new JLabel("Text Only Label");
jlbLabel3 = new JLabel(icon); // Label of Icon Only
add(jlbLabel1);
add(jlbLabel2);
add(jlbLabel3);
}
public static void main(String[] args) {
JFrame frame = new JFrame("jLabel Usage Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setContentPane(new JlabelDemo());
frame.pack();
frame.setVisible(true);
}
}
Output: