Icon in Src folder

て烟熏妆下的殇ゞ 提交于 2019-12-11 23:31:57

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12436851/icon-in-src-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!