使用 getResource() 检索并显示 JList 中的图标
我在制作的 JList 中显示图标时遇到问题。我将路径传递给函数 getResource(),然后将资源存储在 URL 对象中。但是,每次我调用 getResource() 函数时,即使图像存在于目录中,它也会返回 null。
我尝试在 stackoverflow 和其他网站上查找,但找不到解决此问题的方法。我什至尝试将图像存储在项目目录内的多个位置,但无济于事。如果有帮助的话,我正在使用 Eclipse IDE。所以我将文件存储在 /src/、/src/resources/ 和项目文件夹中。
这是我的代码:
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.*;
@SuppressWarnings("serial")
public class JListRenderer extends DefaultListCellRenderer
{
private static ArrayList<CakeRecipe> cRecipes;
private ImageIcon[] images;
// Index of cake recipe
private int cakeIndex;
JListRenderer(ArrayList<CakeRecipe> recipes)
{
cRecipes = recipes;
// Initializes an array of ImageIcons FileHander.jpgCounter() counts the amount of JPG's in the folder, FileHandler.getPath() gives you the filepath
images = new ImageIcon[FileHandler.jpgCounter(new File(FileHandler.getPath()))];
for(int i = 0, j = images.length; i < j; i++)
images[i] = createImageIcon("/resources/" + cRecipes.get(cakeIndex).getPhoto()); // cRecipes.get().getPhoto() gives you the filename of the village only (e.g.: photo.jpg)
}
protected static ImageIcon createImageIcon(String path)
{
URL imgURL = JListRenderer.class.getResource(path);
if(imgURL != null)
return new ImageIcon(imgURL);
else
{
System.err.println("Could not find image. Certain menu elements may not display properly");
return null;
}
}
...
该代码应该工作还是我没有正确使用 URL 或 getResource() ?图像位于目录中,我传递的路径与图像的路径相同。示例:
"/resources/photo.jpg"
任何帮助将不胜感激。谢谢。
编辑:问题出在 Eclipse 上。它没有在构建路径中注册资源。一个简单的“刷新”就解决了问题。感谢大家的帮助。
I'm having problems displaying icons in a JList that I've made. I'm passing a path to the function getResource() and then storing the resource in a URL object. However, every time I call the getResource() function it returns a null, even though the images exist in the directory.
I've tried looking around on stackoverflow and other sites but I can't find a solution to this problem. I even tried storing the images in multiple locations inside my project directory but to no avail. If it helps, I'm using the Eclipse IDE. So I've stored the files in the /src/, /src/resources/, and the project folder.
Here is my code:
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.*;
@SuppressWarnings("serial")
public class JListRenderer extends DefaultListCellRenderer
{
private static ArrayList<CakeRecipe> cRecipes;
private ImageIcon[] images;
// Index of cake recipe
private int cakeIndex;
JListRenderer(ArrayList<CakeRecipe> recipes)
{
cRecipes = recipes;
// Initializes an array of ImageIcons FileHander.jpgCounter() counts the amount of JPG's in the folder, FileHandler.getPath() gives you the filepath
images = new ImageIcon[FileHandler.jpgCounter(new File(FileHandler.getPath()))];
for(int i = 0, j = images.length; i < j; i++)
images[i] = createImageIcon("/resources/" + cRecipes.get(cakeIndex).getPhoto()); // cRecipes.get().getPhoto() gives you the filename of the village only (e.g.: photo.jpg)
}
protected static ImageIcon createImageIcon(String path)
{
URL imgURL = JListRenderer.class.getResource(path);
if(imgURL != null)
return new ImageIcon(imgURL);
else
{
System.err.println("Could not find image. Certain menu elements may not display properly");
return null;
}
}
...
Should this code work or am I not using the URL or getResource() properly? The images are in the directory, and I'm passing a path identical to the image's path. Example:
"/resources/photo.jpg"
Any help would be greatly appreciated. Thank you.
EDIT: The problem was with Eclipse. It didn't register the resources in the build path. A simple "Refresh fixed the problem. Thanks for all your help, everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来应该可以。我尝试使用资源包中的 test.txt 文件运行以下命令:
main 方法中的前两个语句可用于找出运行时被视为相对根的路径。
我已经在 NetBeans 和 Eclipse 中尝试过这一点,并且在这两种情况下都有效。现在,真正的问题是,您的项目设置是什么以及如何构建?源文件和类文件是否有单独的文件夹,或者它们放在一起?您使用 Ant 脚本还是 Eclipse 构建方法?
如果文件仅位于文件夹中而不是包中,则不确定它们是否位于编译路径或运行时类路径上。我怀疑情况可能是后者。尝试确保
resources
是一个包,而不是一个文件夹。尝试从您的项目创建一个 jar 文件,然后检查其内容以查看是否包含您的资源。尝试从 jar 文件运行,看看是否有区别。It looks like it should work. I've tried running the following, with a test.txt file in the resources package:
The first two statements in the main method can be of use to find out what path is considered the relative root at runtime.
I've tried this in NetBeans and Eclipse and in both cases it worked. Now, the real question is, what's your project setup and how do you build? Is there a separate folder for sources and class files or are they put together? Do you use an Ant script or Eclipse build methods?
If you've got the files only in a folder and not in a package, then it's not certain that they're gonna be on the compilation path or on the runtime class path. I suspect the latter may be the case. Try to make sure
resources
is a package, not a folder. Try creating a jar file from your project, then check its contents to see if your resources were included. Try running from the jar file and see if it makes a difference.由于显示所有相关因素很困难,因此可能很难在您的特定情况下确定问题。相反,您可以从这个有效的示例开始,并将其与您自己的实现进行比较。
Becasue it's awkward to show all the relevant factors, it may be difficult to identify the problem in your particular situation. Instead, you might start with this working example and compare it with your own implementation.