Java 如何正确指定文件夹中的图像路径(ImageIcon)
我正在遵循一个关于为JLabel设置图像路径的参考和教程,但无法正确获取路径 我有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
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文件夹。我想我累了。但我很感激你的帮助。谢谢。标签中有更多的例子。