我是新来的挥杆,我试图显示,我用下面的代码拖进我的项目一个简单的图像。一切都编译并运行,但是,图像不显示。爪哇 - 秋千显示图像不工作
顺便说一下,我真的很喜欢这样做,而不是从文件路径中获取图像。
代码:
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ImageProcessorApp IPA = new ImageProcessorApp();
IPA.displayImage();
}
void displayImage() throws IOException {
JFrame frame = new JFrame("frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
BufferedImage wPic = ImageIO.read(this.getClass().getResource("url-2.jpg"));
JLabel wIcon = new JLabel(new ImageIcon(wPic));
frame.add(wIcon);
System.out.println("added image");
}
没有'this.getClass()的getResource( “URL-2.JPG”)'返回一个非空的对象?我的猜测是文件名/路径是错误的。你到底在哪条路径上拖动图像? – cello
是类路径中的图像文件,所以getResource方法可以找到它?上看到类路径是:的System.out.println(System.getProperty( “java.class.path”)); – NormR