我用java编写的一个程序能正常运行,但是生成jar包后程序的图片都显示不了。经过网上查找用ImageIO来加载图片,但程序一直调试不对,求大神帮忙指点一下。下面是显示图片的那一部分的代码。显示的错误是Type mismatch: cannot convert from BufferedImage to ImageIcon。
JLabel labellogo = new JLabel();
ImageIcon imagelogo = new ImageIcon();
try {
imagelogo=ImageIO.read(getClass().getClassLoader().getResource("./images/Logo.jpg"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
labellogo.setIcon(imagelogo);
dialogPanel.add(labellogo,BorderLayout.EAST);
------解决方案--------------------
ImageIcon imagelogo = new ImageIcon();
change to
BufferedImage imagelogo = null;
------解决方案--------------------
你参考一下
package test;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test {
public Test() {
JFrame frame = new JFrame();
JLabel labellogo = new JLabel();
URL url = Test.class.getResource("images/1.PNG");;
System.out.println(url);
ImageIcon icon = new ImageIcon(url);
System.out.println(icon);
labellogo.setIcon(icon);
frame.add(labellogo, BorderLayout.EAST);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
Test t = new Test();
}
}