java imageicon png_java – 为Windows 7显示ImageIcon png文件的正确路径是什么?

我想测试一个带有简单png图像的程序.我写了一个简短的程序来做到这一点,但我似乎无法让路径正确.我已经检查,再次检查,重新检查,并检查了我的路径名称四倍,但没有正确,但无论我做什么,这个图像都不会显示.我使用了Oracle在

ImageIcon文档(creaetImageIcon())中编写的一个简短类来完成此任务,但它似乎没有帮助.我将在下面发布整个程序,因为它很短.

package practiceImages;

import java.awt.BorderLayout;

import java.awt.Toolkit;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class ImageIconGUITest {

public static void main(String[] args) {

ImageIconGUITest gui = new ImageIconGUITest();

gui.display();

}

private ImageIcon createImageIcon(String path, String description) {

java.net.URL imgURL = getClass().getResource(path);

if (imgURL != null) {

return new ImageIcon(imgURL, description);

} else {

System.err.println("Couldn't find file: " + path);

return null;

}

}

private void display() {

JFrame frame = new JFrame();

JLabel label = new JLabel(createImageIcon(

"Users/Evan/javaItems/Sprites_and_Other_Art/green.png", "the color green"));

frame.add(BorderLayout.CENTER, label);

frame.setSize(500, 500);

frame.setVisible(true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值