src获取同级目录中的图片,Src文件夹中的图标

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值