package com.zym.lesson16; import javax.swing.*; import java.awt.*; public class JIconDemo extends JFrame implements Icon { public static void main(String[] args) { new JIconDemo().init(); } private int width; private int height; //无参构造 public JIconDemo(){ } //有参构造 public JIconDemo(int width,int height){ this.height=height; this.width=width; } public void init(){ JIconDemo jIconDemo=new JIconDemo(15,15); JLabel jLabel=new JLabel("icontest",jIconDemo,SwingConstants.CENTER); Container container=getContentPane(); container.add(jLabel); this.setTitle("lesson16-IconDemo"); this.setVisible(true); this.setSize(400,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }; @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillOval(x,y,width,height); } @Override public int getIconWidth() { return this.width; } @Override public int getIconHeight() { return this.height; } }