我想问一下
   Icon icon=new ImageIcon("");
   
    如何把图像加载进去??
    
    谢谢啦~~

解决方案 »

  1.   

    Icon icon=new ImageIcon("图片地址");
    如Icon icon=new ImageIcon("D:\\1.gif");
    有好多方法,去看看API就知道了
      

  2.   

    是啊,可以看看API中的Icon.java和imageIcon.java这两个类文件
    /*
     * @(#)Icon.java 1.17 05/11/17
     *
     * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */
    package javax.swing;import java.awt.Graphics;
    import java.awt.Component;
    /**
     * A small fixed size picture, typically used to decorate components.
     * 
     * @see ImageIcon
     */public interface Icon 
    {
        /**
         * Draw the icon at the specified location.  Icon implementations
         * may use the Component argument to get properties useful for 
         * painting, e.g. the foreground or background color.
         */
        void paintIcon(Component c, Graphics g, int x, int y);
        
        /**
         * Returns the icon's width.
         *
         * @return an int specifying the fixed width of the icon.
         */
        int getIconWidth();    /**
         * Returns the icon's height.
         *
         * @return an int specifying the fixed height of the icon.
         */
        int getIconHeight();
    }
      

  3.   

    必须放到根目录下吗?  我看过API了 但是如果不是放在根目录下 具体的格式应该是怎样的呢?
      

  4.   

    可以用相对路径,将图片复制到工程目录中
    public class Test {
    ImageIcon icon = new ImageIcon(Test.class.getClassLoader().getResource("abc.jpg"));
    }
      

  5.   

    但是放到跟目录可以, 放到下面的文件夹里面 ,利用绝对路径就出问题了,图片就不会显示哦~
    icon可用的图片的格式有哪些呢?