Java Eclipse 图像文件的相对文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19392767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:46:14  来源:igfitidea点击:

Eclipse relative file-path to the image file

javaeclipserelative-path

提问by Alex

I have an annoying problem with the relative file-path on eclipse. I've seen already many posts about how to fix it but nothing works for me.

我在 eclipse 上的相对文件路径有一个烦人的问题。我已经看过很多关于如何修复它的帖子,但对我来说没有任何效果。

My directory structure is the following:

我的目录结构如下:

enter image description here

在此处输入图片说明

Inside of the C.java I I want to indicate the relative path to the NotSet_16x16.png

里面的C.java II要指明NotSet_16x16.png的相对路径

 ip_address_image_label.setIcon (new ImageIcon ("<relativePath>\NotSet_16x16.png"));

采纳答案by alterfox

Don't keep resources with the code. Create a separate 'resources' folder (a sourcefolder along with the src you already have), an images folder in it, and keep those PNGs there. Do the same for the rest of your resources (I see you have a csv and a txt file).

不要在代码中保留资源。创建一个单独的“资源”文件夹(一个文件夹以及您已经拥有的 src),一个图像文件夹,并将这些 PNG 保存在那里。对其余资源执行相同操作(我看到您有一个 csv 和一个 txt 文件)。

When done with that, you will see that you shouldn't access your resources having in mind the location of the class where you need them, but only having in mind that they are resources and all of them are at the same place, i.e. get them with an absolute path like /images/NotSet_16x16.png. So in your class you could do something like:

完成后,您将看到您不应该在考虑需要它们的类的位置的情况下访问您的资源,而只需要记住它们是资源并且它们都在同一个位置,即它们使用绝对路径,如 /images/NotSet_16x16.png。因此,在您的课堂上,您可以执行以下操作:

new ImageIcon(this.getClass().getResource("/images/NotSet_16x16.png"))

回答by Maarten Bodewes

Generally files within the code are used as resources. After compilation they end up with the class files, and finally in a .jarfile.

通常,代码中的文件用作资源。编译后,它们以类文件结束,最后在一个.jar文件中。

So read the data into a byte[]from the Cclass as a resourceand use the ImageIcon(byte[])constructor to create the image.

所以将数据byte[]C类中作为资源入a,并使用ImageIcon(byte[])构造函数来创建图像。



Alternatively you may use an intermediate imagewhich could be read by using an URL to a resourceto create the icon. This is probably more neat as you don't have to bother yourself with reading an InputStreaminto a byte[]buffer.

或者,您可以使用可以通过使用资源的 URL读取的中间图像来创建图标。这可能更简洁,因为您不必费心将 an 读入缓冲区。InputStreambyte[]



Note that you can simply use the name of the file directly if you use either of these methods (as the class file and the image are in the same package). You don't need to specify a path.

请注意,如果您使用这些方法之一(因为类文件和图像在同一个包中),则可以直接使用文件名。您不需要指定路径。

回答by Mike B.

Try this one:

试试这个:

String filePath = ".\images\NotSet_16x16.png";

where ?.\? is a root for Eclipse project, ?images? is a folder with user's files inside of Eclipse project. Since we are talking about Windows OS, we have to use ?\? and not ?/?, like in Linux, but ?\? is the reserved symbol, so we have to type ?\? to get desired result.

在哪里 ?。\?是 Eclipse 项目的根目录吗?图像?是 Eclipse 项目中包含用户文件的文件夹。既然我们在谈论 Windows 操作系统,我们必须使用 ?\? 而不是 ?/?,就像在 Linux 中一样,而是 ?\? 是保留符号,所以我们必须输入 ?\? 得到想要的结果。

回答by A-a-ron

I tried all these answers for my problem, and none of them worked. I asked a friend and he answered my problem perfectly. Create a source folder called Images (ie if you're using eclipse, right-click your project -> new ->sourceFolder). Call it whatever you want, i called my Images. Put some images in it.

我为我的问题尝试了所有这些答案,但都没有奏效。我问了一个朋友,他完美地回答了我的问题。创建一个名为 Images 的源文件夹(即,如果您使用的是 eclipse,请右键单击您的项目 -> new ->sourceFolder)。随便你怎么称呼它,我叫我的图像。在里面放一些图片。

Now i had JLabels where i gave them ImageIcons. Look at the following code.

现在我有了 JLabels,我给了他们 ImageIcons。看下面的代码。

    ImageIcon BPawn;
    ImageIcon WPawn;
    JLabel Label = new JLabel[8][8] //2D array of labels that gives each spot a position.
    public void setPieces(){
        //set up pawns in their respective positions.
        BPawn = new ImageIcon("Images/BPawn.png", "BPawn");
        WPawn = new ImageIcon("Images/WPawn.png", "WPawn");
    for(int i=0;i<Label[0].length;i++){
        Label[1][i].setIcon(BPawn);
        Label[6][i].setIcon(WPawn);
    }//end for

    }//end setPieces.

There is a lot more in setPieces() method, but this glimpse is how you would reference the images in your source folder when you create an executable jar and want the images to show up.

setPieces() 方法还有很多,但这个一瞥是您在创建可执行 jar 并希望图像显示时如何引用源文件夹中的图像。