Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确指定文件夹中的图像路径(ImageIcon)_Java_Swing_Embedded Resource_Imageicon - Fatal编程技术网

Java 如何正确指定文件夹中的图像路径(ImageIcon)

Java 如何正确指定文件夹中的图像路径(ImageIcon),java,swing,embedded-resource,imageicon,Java,Swing,Embedded Resource,Imageicon,我正在遵循一个关于为JLabel设置图像路径的参考和教程,但无法正确获取路径 我有 String pathToUserPictureImage = "resources/Assets/studentPhoto.png"; ImageIcon userPicture = new ImageIcon(getClass().getClassLoader().getResource(pathToUserPictureImage)); JLabel userPictureImage

我正在遵循一个关于为JLabel设置图像路径的参考和教程,但无法正确获取路径

我有

String pathToUserPictureImage = "resources/Assets/studentPhoto.png";
    ImageIcon userPicture = new ImageIcon(getClass().getClassLoader().getResource(pathToUserPictureImage));
        JLabel userPictureImagePlaceHolder = new JLabel(userPicture, HEIGHT);
我在项目中的文件夹包括:

当我运行它时,它将返回一条关于空指针的错误消息,我怀疑这是提供的路径有问题


如果您对我的代码有任何建议和更正,我将不胜感激。我最近刚从拖放组件切换到手工编写代码。

/Assets/studentPhoto.png-我在源代码中看不到任何
资源
目录tree@MadProgrammer谢谢糟糕的是,当我放入字符串pathToUserPictureImage=“Assets/studentPhoto.png”时,我不必放入它工作的“资源”;不必从项目中添加src文件夹。我想我累了。但我很感激你的帮助。谢谢。标签中有更多的例子。