是看论坛其他人介绍的《疯狂java实战演义》,强化java se基本功的第3章图像浏览器时候碰到的,问题我抽出来,就BMP类图片不加载,只要上面那个简单的加载能搞定了,剩下的也就解决了!

解决方案 »

  1.   

    java.io.File file=new java.io.File("jiu.bmp");
        try{
        java.awt.Image image = javax.imageio.ImageIO.read(file);
        
        javax.swing.ImageIcon icon = new javax.swing.ImageIcon(image);
        new SetIconTest().getLabel().setIcon(icon);
        }catch(java.io.IOException e){}
      

  2.   

    可以加载非BMP外的任意图,求明白!
      

  3.   

    论坛就是没人给说句话,今天自己查阅源代码找到原因了,在此留言给大家分享了:ImageIcon构造方法的问题 public ImageIcon(String filename, String description) {
            image = Toolkit.getDefaultToolkit().getImage(filename);
            if (image == null) {
                return;
            }
            this.filename = filename;
            this.description = description;
            loadImage(image);
        }
    getImagepublic abstract Image getImage(String filename)
    Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG. The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image. 
    Since the mechanism required to facilitate this sharing of Image objects may continue to hold onto images that are no longer in use for an indefinite period of time, developers are encouraged to implement their own caching of images by using the createImage variant wherever available. If the image data contained in the specified file changes, the Image object returned from this method may still contain stale information which was loaded from the file after a prior call. Previously loaded image data can be manually discarded by calling the flush method on the returned Image. This method first checks if there is a security manager installed. If so, the method calls the security manager's checkRead method with the file specified to ensure that the access to the image is allowed. 
    Parameters:
    filename - the name of a file containing pixel data in a recognized file format. 
    Returns:
    an image which gets its pixel data from the specified file. 
    Throws: 
    SecurityException - if a security manager exists and its checkRead method doesn't allow the operation.
    See Also:
    createImage(java.lang.String)所以问题就纠结在这里了,找到老根了。现在改成这样了总算是可以显示了:
    try
    {
    BufferedImage bi = ImageIO.read(new File("1.bmp"));

    ImageProducer producer = bi.getSource();

    Toolkit toolkit = Toolkit.getDefaultToolkit();

    Image image = toolkit.createImage(producer); ImageIcon ic = new ImageIcon(image); new SetIconTest().getLabel().setIcon(ic);
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }结贴!!!!!!!!!(查阅的时候曾经翻到论坛2001年都有人问过这样问题也是没答案,纠结啊,还有非要什么双关注才能发私信请人帮忙,累啊!)