java中的icon是属于接口类,主要设置窗口图标,实例如下:
import java.awt.*;
import javax.swing.*;
public class DrawIcon implements Icon{//该类实现该接口icon
private int width;
private int height;
@Override
public int getIconHeight(){
return this.height;
}
@Override
public int getIconWidth(){
return this.width;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y){
g.setColor(Color.red);
g.fillOval(x, y, width, height);
}
public DrawIcon(int width, int height){
this.width=width;
this.height=height;
}
public static void main(String[] args){
DrawIcon icon=new DrawIcon(15,15);
JLabel jl=new JLabel("测试",icon,SwingConstants.CENTER);
JFrame jf=new JFrame();
Container c=jf.getContentPane();
c.add(jl);
jf.setVisible(true);
jf.setSize(300,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}