简体   繁体   English

ImageIcon如何获取要在Java中使用的图像的文件路径?

[英]ImageIcon how to get filepath of image for use in java?

I am trying to use ImageIcon with Java Swing in order to power a simple Java game. 我正在尝试将ImageIcon与Java Swing结合使用,以便为一个简单的Java游戏提供动力。 I would like understand how to call a relative filepath from the eclipse workspace inside a project folder? 我想了解如何从项目文件夹内的Eclipse工作区调用相对文件路径? What is the code that can be used to implement the relative filepath in eclipse for an image resource? 可用于在Eclipse中为图像资源实现相对文件路径的代码是什么?

Use: 采用:

getClass().getResource()

Something like this: 像这样:

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images.jpg")));

If images is in your project folder. 如果images在您的项目文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM