I am trying to create an icon for a jLabel using an image in my src folder.
I am not sure of the file path I would use when creating this icon.
if (CoverImg.equals("")) {
Icon noImage = new ImageIcon("/animedb/NoImage.jpg");
CoverImageLabel.setIcon(noImage);
CoverImageLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
else {
Icon icon = new ImageIcon(CoverImg);
height = icon.getIconHeight();
width = icon.getIconWidth();
CoverImageLabel.setIcon(icon);
CoverImageLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
CoverImageLabel is the jLabel
CoverImg is a String that contains a file path obtained from a database. If there is no file path available I want it to display the NoImage.jpg which is in my src folder. I am using Netbeans IDE, if that is at all relevant.
Can anyone help?
解决方案CoverImageLabel.setIcon(new ImageIcon(TYPE_YOUR_CLASS_NAME_HERE.class.getResource("/animedb/NoImage.jpg")));
As you can see I merged them all into one line, but you can create variables and separate them into multiple lines. Good luck, let me know if there are any issues.