package com.lzw;import java.awt.*;import javax.swing.*;public class DrawIcon implements Icon { private int width; private int height; public int getIconHeight() { // 实现getIconHeight()方法 return this.height; } public int getIconWidth() { // 实现getIconWidth()方法 return this.width; } public DrawIcon(int width, int height) { // 定义构造方法 this.width = width; this.height = height; } public void paintIcon(Component arg0, Graphics arg1, int x, int y) { arg1.fillOval(x, y, width, height); // 绘制一个圆形 } public static void main(String[] args) { DrawIcon icon = new DrawIcon(15, 15); JLabel j = new JLabel("测试", icon, SwingConstants.CENTER); JFrame jf = new JFrame(); Container c = jf.getContentPane(); c.add(j); jf.setSize(1000,1000); jf.setVisible(true); jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); }}程序中没有运行icon.paintIcon();方法的语句,但是后面”JLabel j = new JLabel("测试", icon, SwingConstants.CENTER);“中,直接调用了icon,而且程序结果也直接绘出了圆形图标,那么这里是在实例化DrawIcon类对象icon之后,系统默认自动调用并且运行了类中重写的接口中paintIcon();方法吗?求大神解答啊
添加回答
举报
0/150
提交
取消