是啊,可以看看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(); }
必须放到根目录下吗? 我看过API了 但是如果不是放在根目录下 具体的格式应该是怎样的呢?
可以用相对路径,将图片复制到工程目录中 public class Test { ImageIcon icon = new ImageIcon(Test.class.getClassLoader().getResource("abc.jpg")); }
如Icon icon=new ImageIcon("D:\\1.gif");
有好多方法,去看看API就知道了
/*
* @(#)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();
}
public class Test {
ImageIcon icon = new ImageIcon(Test.class.getClassLoader().getResource("abc.jpg"));
}
icon可用的图片的格式有哪些呢?