如何在Java中引用本地图像? [英] How to reference a local image in java?

查看:92
本文介绍了如何在Java中引用本地图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jLabel5.setIcon(new javax.swing.ImageIcon("./i/login.png"));

我正在尝试引用该图片。路径正确,并且图像实际存在。当我使用完整路径时(例如, C:/ blah blah,它可以工作,但这不行吗?

I'm trying to reference that image. The path is correct, and the image actually exists. When I use the full path (I.E. "C:/ blah blah" it works, but this doesn't?

图像文件夹位于bin文件夹中。

The image folder is in the bin folder.

推荐答案

//This will retuns the URL of the image file inside your project
  this.getClass().getResource("/i/login.png");

因此,您的代码将是:

URL imageUrl = this.getClass().getResource("/i/login.png");
jLabel5.setIcon(new javax.swing.ImageIcon(imageUrl));

如果图像在您当前的软件包之外,请以 /i/login.png 开头的路径,否则不需要 /

If the image is outside your current package, start the path with /i/login.png, else, don't need the /.

这篇关于如何在Java中引用本地图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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