博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java添加资源文件_如何在eclipse中将资源文件夹添加到我的Java项目中
阅读量:1530 次
发布时间:2019-04-21

本文共 1366 字,大约阅读时间需要 4 分钟。

我想要有一个地方存储我的图像文件,以便在我的java项目中使用(一个真正简单的类,只是将图像加载到面板上)。我看到无处不在,找不到如何做到这一点。我该如何做?我已经尝试向项目添加一个新文件夹,向项目添加一个新的类文件夹,并向项目添加一个新的源文件夹。无论我做什么,我总是得到一个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);

}

}

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

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

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

转载地址:http://vkudy.baihongyu.com/

你可能感兴趣的文章
bash学习笔记
查看>>
sqlite学习
查看>>
手把手教你实现Unity与Android的交互
查看>>
Unity3D摄像机裁剪——NGUI篇
查看>>
lua深拷贝一个table
查看>>
app运行提示Unable to Initialize Unity Engine
查看>>
spring boot 与 Ant Design of Vue 实现修改按钮(十七)
查看>>
spring boot 与 Ant Design of Vue 实现删除按钮(十八)
查看>>
spring boot 与 Ant Design of Vue 实现角色管理布局以及角色的列表(十九)
查看>>
spring boot 与 Ant Design of Vue 实现新增角色(二十)
查看>>
spring boot 与 Ant Design of Vue 实现修改角色(二十一)
查看>>
spring boot 与 Ant Design of Vue 实现删除角色(补二十一)
查看>>
spring boot 与 Ant Design of Vue 实现组织管理布局的实现(二十二)
查看>>
spring boot 与 Ant Design of Vue 实现左侧组织树(二十三)
查看>>
spring boot 与 Ant Design of Vue 实现新增组织(二十四)
查看>>
spring boot 与 Ant Design of Vue 实现修改组织(二十五)
查看>>
spring boot 与 Ant Design of Vue 实现删除组织(二十六)
查看>>
spring boot 与 Ant Design of Vue 实现获取用户列表(二十七)
查看>>
带你玩转属于自己自己的spring-boot-starter系列(二)
查看>>
什么是服务熔断?
查看>>