Java - ImageIcon不会显示图像 [英] Java - ImageIcon won't show image

查看:83
本文介绍了Java - ImageIcon不会显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

searchIcon = new ImageIcon( "searchIcon.png" );

searchIconLabel = new JLabel();
searchIconLabel.setIcon(searchIcon);

....

searchHorizontalPanel.add( searchIconLabel );

我正在尝试加载图标,但它不会显示。 jpanel在boxlayout内。我不知道这是否会让事情变得混乱。这很奇怪,因为除了图标图像之外我还可以添加其他任何东西。我浏览了调试器,searchIcon显示宽度和高度为-1。这是否意味着图像未被加载?

I'm trying to load the icon but it won't show up. The jpanel is within a boxlayout. I don't know if that messes things up. It's strange because I can add pretty much anything else except icon images. I went through the debugger and searchIcon is showing the width and height to be -1. Does this mean the image is not being loaded?

推荐答案

您的图像文件需要与运行图像文件位于同一目录中来自。

Your image file needs to be in the same directory as you are running the application from.

如果它实际上与拨打电话的班级在同一目录中,那么你可以使用:

If its actually in the same directory as the class making the call then you can use:

searchIcon = new ImageIcon( getClass().getResource("searchIcon.png") );

如果它位于某个任意位置,那么您需要指定完整路径。

If it is in some arbitrary location then you will need to specify the full path.

这篇关于Java - ImageIcon不会显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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