图像不是在jar中显示而是在编译器中显示? [英] Images are not displaying in jar but displaying in compiler?

查看:82
本文介绍了图像不是在jar中显示而是在编译器中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我知道,之前有人问过。但是我看过的每一个资源都使用IconImages,而我只有普通的图像。

I know, I know, this has been asked before. But every resource I've looked at uses IconImages while I just have plain Images.

有不同的解决方案吗?请帮忙,因为我一直在研究,并试图解决这个问题几天没有进展。

Is there a different solution? Please help as I've been stuck researching and trying to figure this out for days now with no progress.

Image Floor = Toolkit.getDefaultToolkit().getImage("Floor.PNG");

编辑:如果我要确保jar不会压缩,我创建了一个单独的目录这个代码可以运行吗?

If I was to make sure the jar wouldn't compress and I created a seperate directory in the jar for images and put the correct file path, would this code work?

推荐答案

工具包#getImage(String s)查找一个文件,你的图像可能在Jar中,现在是资源而不是文件。看看为此使用资源。

Toolkit#getImage(String s) looks for a file and likely your image is in the Jar and is now a resource not a file. Look to using resources for this.

注意ImageIO.read(...)可以接受一个I​​nput类的参数,Class类有一个方法,getResourceAsStream(...)它可以将资源放入ImageIO可以读取的Stream中。试一试。

Note that ImageIO.read(...) can accept an InputStream parameter the Class class has a method, getResourceAsStream(...) which can put the resource into a Stream that ImageIO can read. Give that a try.

另外,当你尝试做什么时,你收到任何错误信息吗?

Also, are you getting any error messages when you try what you're doing?

这篇关于图像不是在jar中显示而是在编译器中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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