Java 从相对路径获取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1978445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get image from relative path
提问by Chan Pye
In my project I have 2 packages. images- contain images and notification- contain java files
在我的项目中,我有 2 个包。 图像- 包含图像和 通知- 包含 java 文件
In notification/main.java I get Image object from image using this code
在通知/main.java 中,我使用此代码从图像中获取图像对象
Image image = Toolkit.getDefaultToolkit().getImage("/images/key-16x16.png");
and I can't get image.
我无法获得图像。
How can I fix this bug.
我该如何修复这个错误。
采纳答案by Chan Pye
I'm using Netbeans to develop Java desktop application and I have solved my problem.
我正在使用 Netbeans 开发 Java 桌面应用程序,我已经解决了我的问题。
Image image = new ImageIcon(this.getClass().getResource("/images/bell-icon16.png")).getImage();
"this" is a class extendsJFrame
“this”是一个扩展JFrame的类
回答by Adeel Ansari
/
means an absolute path, save Java web-apps where /
means relative to context. So, I would suggest to use relative URL, means get rid of that /
in front, and then provide the right path.
/
表示绝对路径,保存 Java 网络应用程序,其中/
表示相对于上下文。所以,我建议使用相对URL,意思是去掉/
前面的那个,然后提供正确的路径。
In case, even then you can't solve it, try to create a file on the same path you are looking for image. This way you will know that where you are looking exactly and where you should look.
万一,即使这样您也无法解决它,请尝试在您正在查找图像的同一路径上创建一个文件。这样你就会知道你正在看的地方和你应该看的地方。
回答by Bozho
You could also try
你也可以试试
Image image = new ImageIcon(path).getImage();
回答by David Okwii
i've also been on this and it turns out that you need to create a package inside of your src folder. for instace if you create a package called images inside of the src folder, your relative path will be /images/yourimage.png.
我也一直在研究这个,结果证明你需要在你的 src 文件夹中创建一个包。例如,如果您在 src 文件夹中创建一个名为 images 的包,则您的相对路径将是 /images/yourimage.png。
Notice that the slash(/) must be there! more info here http://forums.macrumors.com/showthread.php?t=533922
注意斜线(/)必须在那里!更多信息在这里http://forums.macrumors.com/showthread.php?t=533922
it worked for me
它对我有用
回答by daboross
Toolkit tk = Toolkit.getDefaultToolkit();
Class<? extends JFrame> j = YOURJFRAME.getClass();
Image image = tk.createImage(j.getResource("/images/bell-icon16.png"));
Try that code, if you have a JFrame that will work. If you have an Applet, then just use
试试那个代码,如果你有一个可以工作的 JFrame。如果您有 Applet,则只需使用
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.createImage("images/bell-icon16.png");
With applets you never want to use the / in the beginning, but if you have a JFrame, and you are using getResource, you need the / in the beginning of the Path.
对于小程序,您永远不想在开头使用 /,但是如果您有 JFrame,并且正在使用 getResource,则需要在路径的开头使用 /。
回答by Davut Gürbüz
SwingResourceManager.getImage(YourClass.class,"key-16x16.png");
The getIcon
method will return Icon as similar
该getIcon
方法将返回 Icon 类似
回答by benscabbia
Incase the solution above doesn't work, try this (which worked for me):
如果上面的解决方案不起作用,试试这个(对我有用):
Image image = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("/Images/bell-icon16.png"));
回答by shiran
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("../resources/MainIcon.png")));