The problem is that the image is not getting loaded while I run the following swing program.
I have a package called "sWINGPRAC" inside which I have a JAVA file IconLabelDemo.java. I have ensured that the Image "myIcon.gif" is in the same directory.
IconLabelDemo.java.
package sWINGPRAC;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class IconLabelDemo {
public IconLabelDemo() {
JFrame jfrm = new JFrame("ImageIcon");
jfrm.getContentPane().setLayout(new GridLayout(4, 1));
jfrm.setSize(250, 300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon("myIcon.gif");
JLabel jlabIcon = new JLabel(img);
JLabel jlabIconTxt = new JLabel("Default iCon and text position",img , SwingConstants.CENTER);
JLabel jlabIconTxt2 = new JLabel("Text left of icon",img,SwingConstants.CENTER);
jlabIconTxt2.setHorizontalTextPosition(SwingConstants.LEFT);
JLabel jlabIconTxt3 = new JLabel("Text Over ICon",img,SwingConstants.CENTER);
jlabIconTxt3.setVerticalTextPosition(SwingConstants.TOP);
jfrm.add(jlabIcon);
jfrm.add(jlabIconTxt);
jfrm.add(jlabIconTxt2);
jfrm.add(jlabIconTxt3);
jfrm.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new IconLabelDemo();
}
});
}
}