Src文件夹中的图标 [英] Icon in Src folder
问题描述
我正在尝试使用src文件夹中的图像为jLabel创建一个图标。
我不确定创建此图标时会使用的文件路径。
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是jLabel
CoverImageLabel is the jLabel
CoverImg是一个String,包含从数据库获取的文件路径。如果没有可用的文件路径,我希望它显示在我的src文件夹中的 NoImage.jpg
。我正在使用Netbeans IDE,如果这完全相关的话。
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.
任何人都可以帮忙吗?
推荐答案
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.
这篇关于Src文件夹中的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!