NB项目内部的图像参考



好的,我不确定我在这里做错了什么,但我不能在我的NB Java项目中引用图像。我的代码基本上与此相同:

    ImageIcon i = new ImageIcon("Resources/character.png");

我的src中有一个名为Resources的包,该包中有character.png,那么我做错了什么?

如果您的文件在类路径(例如<project>/src/Resources/character.png)内,则将其加载为:

这里有一个例子:

public class Example
{
    public ImageIcon loadImage()
    {
        // Note the '/'
        final String path = "/Resources/character.png"; 
        // Load the image as a resource
        ImageIcon icon = new ImageIcon(this.getClass().getResource(path));
        // Return the result
        return icon;
    }
}
// ...
Example e = new Example();
ImageIcon icon = e.loadImage();

最新更新