ImageIcon是Icon接口的一个实现类。网络
ImageIcon类的构造函数:函数
ImageIcon类的经常使用方法:spa
ImageIcon实现的是code
一、设置JLabe、JButton的图标对象
1 ImageIcon imageIcon=new ImageIcon("./image/1.png"); 2 3 JLabel label1=new JLabel(imageIcon); 4 JLabel label2=new JLabel("test",imageIcon,SwingConstants.CENTER); 5 6 JButton button1=new JButton(imageIcon); 7 JButton button2=new JButton("提交",imageIcon);
./表示项目的根目录。blog
二、设置程序左上角的图标接口
1 ImageIcon imageIcon=new ImageIcon("./image/1.png"); 2 frame.setIconImage(imageIcon.getImage()); 3 4 /* 5 参数是Image抽象类的对象。ImageIcon实现的是Icon接口 ,并无实现Image抽象类。 6 须要使用getImage()获取Image对象 7 */
JFrame、JDialog都可使用此种方式设置窗口左上角的图标。图片
此图标就是程序在任务栏显示的图标。get