如何在Eclipse中向我的Java项目添加资源文件夹

时间:2021-09-29 20:20:04

I want to have a place to store my image files to use in my Java project (a really simple class that just loads an image onto a panel). I have looked everywhere and cannot find how to do this. How do I do this?

我希望有一个地方来存储我的图像文件,以便在我的Java项目中使用(一个非常简单的类,只是将图像加载到面板上)。我到处寻找,无法找到如何做到这一点。我该怎么做呢?

I have tried adding a new folder to the project, adding a new class folder to the project, and adding a new source folder to the project. No matter what I do, I always get a IOException. The folders always say they are on the build path, so I'm not sure what to do.

我尝试将新文件夹添加到项目中,向项目添加新的类文件夹,并向项目添加新的源文件夹。无论我做什么,我总是得到一个IOException。文件夹总是说它们在构建路径上,所以我不知道该怎么做。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PracticeFrame extends JFrame{

private static BufferedImage image;
Thread thread;

public PracticeFrame() {
    super();
    setPreferredSize(new Dimension(640,480));
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

public static void main (String[] args) {
    PracticeFrame pframe = new PracticeFrame();
    try {
        image = ImageIO.read(new File("/islands.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    JPanel panel = new JPanel() {
        @Override
        protected void  paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image,0,0,null);
        }
    };

    panel.setBackground(Color.BLUE);
    panel.repaint();
    pframe.add(panel);


}


}

EDIT: Something that worked for me, and I have no idea why, was adding the main/res/ folder as a class folder and then removing it. I ran it while the /main/res/ was part of the build path as a class folder and it still didn't work. When i added it, i got a popup that told me something about excluded filters. But when i removed the folder from the libraries in the build path, and changed my file path to:

编辑:对我有用的东西,我不知道为什么,将main / res /文件夹添加为类文件夹,然后将其删除。我运行它时/ main / res /是构建路径的一部分作为类文件夹,它仍然无法正常工作。当我添加它时,我有一个弹出窗口,告诉我有关排除过滤器的信息。但是,当我从构建路径中的库中删除该文件夹,并将我的文件路径更改为:

image = ImageIO.read(new File("src/main/res/islands.png"));

I at least stopped getting the IOException thrown. I must not be adding the image to the panel correctly, because it's not showing up, but at least it found the file (I think).

我至少停止了抛出IOException。我不能正确地将图像添加到面板,因为它没有显示,但至少它找到了文件(我认为)。

4 个解决方案

#1


57  

For add resoure folder, Build Path -> Configure Build Path -> Source (Tab) -> Add Folder -> Create new Folder 如何在Eclipse中向我的Java项目添加资源文件夹

对于add resoure文件夹,Build Path - > Configure Build Path - > Source(Tab) - > Add Folder - > Create new Folder

add "my-resource.txt" file inside the new folder. Then in your code:

在新文件夹中添加“my-resource.txt”文件。然后在你的代码中:

    InputStream res =
    Main.class.getResourceAsStream("/my-resource.txt");

    BufferedReader reader =
        new BufferedReader(new InputStreamReader(res));
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    reader.close();

#2


7  

To answer your question posted in the title of this topic...

要回答您在本主题标题中发布的问题...

Step 1--> Right Click on Java Project, Select the option "Properties" 如何在Eclipse中向我的Java项目添加资源文件夹

步骤1 - >右键单击Java Project,选择“属性”选项

Step 2--> Select "Java Build Path" from the left side menu, make sure you are on "Source" tab, click "Add Folder" 如何在Eclipse中向我的Java项目添加资源文件夹

步骤2 - >从左侧菜单中选择“Java Build Path”,确保您在“Source”选项卡上,单击“Add Folder”

Step 3--> Click the option "Create New Folder..." available at the bottom of the window 如何在Eclipse中向我的Java项目添加资源文件夹

步骤3 - >单击窗口底部的“创建新文件夹...”选项

Step 4--> Enter the name of the new folder as "resources" and then click "Finish" 如何在Eclipse中向我的Java项目添加资源文件夹

步骤4 - >输入新文件夹的名称作为“资源”,然后单击“完成”

Step 5--> Now you'll be able to see the newly created folder "resources" under your java project, Click "Ok", again Click "Ok"
如何在Eclipse中向我的Java项目添加资源文件夹

步骤5 - >现在,您将能够在java项目下看到新创建的文件夹“resources”,单击“确定”,再次单击“确定”

Final Step --> Now you should be able to see the new folder "resources" under your java project
如何在Eclipse中向我的Java项目添加资源文件夹

最后一步 - >现在您应该能够在java项目下看到新文件夹“resources”

#3


0  

After adding a resource folder try this :

添加资源文件夹后试试这个:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("test.png");

try {
    image = ImageIO.read(input);
} catch (IOException e) {
    e.printStackTrace();
}

#4


0  

Try To Give Full path for reading image.

尝试为阅读图像提供完整的途径。

Example image = ImageIO.read(new File("D:/work1/Jan14*/src/Strawberry.jpg"));

示例image = ImageIO.read(新文件(“D:/work1/Jan14*/src/Strawberry.jpg”));

your code is not producing any exception after giving the full path. If you want to just read an image file in java code. Refer the following - http://docs.oracle.com/javase/tutorial/2d/images/examples/LoadImageApp.java

在给出完整路径后,您的代码不会产生任何异常。如果您只想在java代码中读取图像文件。请参阅以下内容 - http://docs.oracle.com/javase/tutorial/2d/images/examples/LoadImageApp.java

If the object of your class is created at end your code works fine for me and displays the image

如果您的类的对象在最后创建,您的代码可以正常工作并显示图像

// PracticeFrame pframe = new PracticeFrame();//comment this

new PracticeFrame().add(panel);

#1


57  

For add resoure folder, Build Path -> Configure Build Path -> Source (Tab) -> Add Folder -> Create new Folder 如何在Eclipse中向我的Java项目添加资源文件夹

对于add resoure文件夹,Build Path - > Configure Build Path - > Source(Tab) - > Add Folder - > Create new Folder

add "my-resource.txt" file inside the new folder. Then in your code:

在新文件夹中添加“my-resource.txt”文件。然后在你的代码中:

    InputStream res =
    Main.class.getResourceAsStream("/my-resource.txt");

    BufferedReader reader =
        new BufferedReader(new InputStreamReader(res));
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    reader.close();

#2


7  

To answer your question posted in the title of this topic...

要回答您在本主题标题中发布的问题...

Step 1--> Right Click on Java Project, Select the option "Properties" 如何在Eclipse中向我的Java项目添加资源文件夹

步骤1 - >右键单击Java Project,选择“属性”选项

Step 2--> Select "Java Build Path" from the left side menu, make sure you are on "Source" tab, click "Add Folder" 如何在Eclipse中向我的Java项目添加资源文件夹

步骤2 - >从左侧菜单中选择“Java Build Path”,确保您在“Source”选项卡上,单击“Add Folder”

Step 3--> Click the option "Create New Folder..." available at the bottom of the window 如何在Eclipse中向我的Java项目添加资源文件夹

步骤3 - >单击窗口底部的“创建新文件夹...”选项

Step 4--> Enter the name of the new folder as "resources" and then click "Finish" 如何在Eclipse中向我的Java项目添加资源文件夹

步骤4 - >输入新文件夹的名称作为“资源”,然后单击“完成”

Step 5--> Now you'll be able to see the newly created folder "resources" under your java project, Click "Ok", again Click "Ok"
如何在Eclipse中向我的Java项目添加资源文件夹

步骤5 - >现在,您将能够在java项目下看到新创建的文件夹“resources”,单击“确定”,再次单击“确定”

Final Step --> Now you should be able to see the new folder "resources" under your java project
如何在Eclipse中向我的Java项目添加资源文件夹

最后一步 - >现在您应该能够在java项目下看到新文件夹“resources”

#3


0  

After adding a resource folder try this :

添加资源文件夹后试试这个:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("test.png");

try {
    image = ImageIO.read(input);
} catch (IOException e) {
    e.printStackTrace();
}

#4


0  

Try To Give Full path for reading image.

尝试为阅读图像提供完整的途径。

Example image = ImageIO.read(new File("D:/work1/Jan14*/src/Strawberry.jpg"));

示例image = ImageIO.read(新文件(“D:/work1/Jan14*/src/Strawberry.jpg”));

your code is not producing any exception after giving the full path. If you want to just read an image file in java code. Refer the following - http://docs.oracle.com/javase/tutorial/2d/images/examples/LoadImageApp.java

在给出完整路径后,您的代码不会产生任何异常。如果您只想在java代码中读取图像文件。请参阅以下内容 - http://docs.oracle.com/javase/tutorial/2d/images/examples/LoadImageApp.java

If the object of your class is created at end your code works fine for me and displays the image

如果您的类的对象在最后创建,您的代码可以正常工作并显示图像

// PracticeFrame pframe = new PracticeFrame();//comment this

new PracticeFrame().add(panel);