凡把“资源”日食日程表 [英] Where put "resources" dir on eclipse

查看:122
本文介绍了凡把“资源”日食日程表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是一个eclipse Java项目,它的结构是



\Game\src



但是这个代码返回一个例外:

  image = new ImageIcon(new URL(resources\\\Monster.png)); 

java.net.MalformedURLException:no protocol:resources\Monster.png



有什么问题?

解决方案

在eclipse中,使 / code>文件夹一个Source文件夹,以便在启动应用程序时将文件添加到类路径。



然后使用

  image = new ImageIcon(YourClass.class.getResource(/ monster.png)); 

其中 YourClass 是您的课程。



类#getResource(String) 调用



< blockquote>

查找具有给定名称的资源。


这是通过在应用程序的基于javadoc中描述的一些命名规则的类路径。


My project is a eclipse Java project and it structure is

\Game\src

\Game\resources

But this code returns a exception:

image = new ImageIcon(new URL("resources\\Monster.png"));

java.net.MalformedURLException: no protocol: resources\Monster.png

What's wrong?

解决方案

In eclipse, make the resources folder a Source folder so that it adds the files inside it to the classpath when it launches your application.

Then use

image = new ImageIcon(YourClass.class.getResource("/monster.png"));

where YourClass is your class.

The Class#getResource(String) call

Finds a resource with a given name.

It does this by looking for it in your application's classpath based on some naming rules described in the javadoc.

这篇关于凡把“资源”日食日程表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆