Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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/9/three.js/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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项目中,在哪里存储图像等文件?_Java - Fatal编程技术网

在Java项目中,在哪里存储图像等文件?

在Java项目中,在哪里存储图像等文件?,java,Java,我正在尝试访问一个.jpg文件作为背景,如下所示: JLabel label = new JLabel(new ImageIcon("star_wars.jpg"));; 如果我将图片的路径更改为我计算机上包含该文件的目录,它会工作,但我想发布该程序,这意味着它只会显示在我的PC上。我尝试将其存储在项目本身的多个区域中,但没有任何运气。它当前位于名为resources的源文件夹中。我该怎么办?相对于类文件存储它。在本例中,您将把图像文件放在与类文件相同的位置 ImageIcon icon =

我正在尝试访问一个.jpg文件作为背景,如下所示:

JLabel label = new JLabel(new ImageIcon("star_wars.jpg"));;

如果我将图片的路径更改为我计算机上包含该文件的目录,它会工作,但我想发布该程序,这意味着它只会显示在我的PC上。我尝试将其存储在项目本身的多个区域中,但没有任何运气。它当前位于名为resources的源文件夹中。我该怎么办?

相对于类文件存储它。在本例中,您将把图像文件放在与类文件相同的位置

ImageIcon icon = new ImageIcon(MyClass.class.getResource("star_wars.jpg"));

除非您使用的是引擎,否则没有
resources
文件夹,除非您自己创建一个


相对路径(与您的路径一样)通常是相对于包文件夹的,但是在Netbeans中,它们是相对于项目文件夹的。(不确定其他IDE)

+1或通过在路径开头添加
/
来相对于JAR的顶层。感谢你们两位!