来自src文件夹的Toolkit.getImage() [英] Toolkit.getImage() from src folder

查看:102
本文介绍了来自src文件夹的Toolkit.getImage()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从包中的src /文件夹中获取图像,但是我没有取得任何成功。

I am trying to get an image from the src/ folder in a package, however I am not having any success.

nekoPics[i] = tk.getImage(getClass().getResource(String.format("resources/pracs/neko/%s", nekosrc[i])));

任何人的消费?

推荐答案

是否将其复制到构建代码的任何位置?如果在执行时该类位于 bin 目录(或jar)中,则位于 src 目录中无效文件)。

Is it being copied into wherever your code is being built? Being in the src directory doesn't help if at execution time the class is in a bin directory (or a jar file).

另请注意,如果您的类在一个包中,那么 Class.getResource 将相对于那个包默认。因此,如果您的软件包是foo.bar,那么它将寻找 foo / bar / resources / pracs / neko / whatever 。如果你想让它绝对,你可以使用 ClassLoader.getResource ,或者在开头放一个领先的 / 你的字符串:

Also note that if your class is in a package, then Class.getResource will work relative to that package by default. So if your package is foo.bar, it'll be looking for foo/bar/resources/pracs/neko/whatever. If you want to make it absolute, you could either use ClassLoader.getResource, or put a leading / at the start of your string:

String resource = String.format("/resources/pracs/neko/%s", nekosrc[i]);
nekoPics[i] = tk.getImage(getClass().getResource(resource));

String resource = String.format("resources/pracs/neko/%s", nekosrc[i]);
nekoPics[i] = tk.getImage(getClass().getClassLoader().getResource(resource));

当然,如果 reousrces 目录是实际上相对于你的包,你不应该那样做:)

Of course, if the reousrces directory is actually relative to your package, you shouldn't do that :)

这篇关于来自src文件夹的Toolkit.getImage()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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