如何在eclipse中将资源文件夹添加到我的Java项目中 [英] How do I add a resources folder to my Java project in eclipse
问题描述
import java.awt。颜色;
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;
线程;
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);
}
}
编辑:对我有用的东西,我不知道为什么要添加主/ res /文件夹作为类文件夹,然后删除它。我运行它,而/ main / res /作为构建路径的一部分作为一个类文件夹,它仍然没有工作。当我添加它,我有一个弹出窗口告诉我一些关于排除的过滤器。但是当我从构建路径中的库中删除文件夹,并将文件路径更改为:
image = ImageIO.read (new File(src / main / res / islands.png));
我至少停止得到抛出的IOException。我不能正确地将图像添加到面板,因为它没有显示,但至少它找到了文件(我想)。
对于添加资源文件夹,
构建路径 - >配置构建路径 - >源(选项卡) - >添加文件夹 - >创建新文件夹
在新文件夹中添加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();
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? 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.
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:
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).
For add resoure folder, 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:
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();
这篇关于如何在eclipse中将资源文件夹添加到我的Java项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!