NB 项目中的图像引用?

Image referencing inside a NB project?

好的,我不确定我在这里做错了什么,但我无法在我的 NB Java 项目中引用图像。我的代码基本上是这样的:

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

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

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

这是一个例子:

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();