Java 扩展ImageIcon类并保持兼容性

Java 扩展ImageIcon类并保持兼容性,java,swing,jlabel,scaling,imageicon,Java,Swing,Jlabel,Scaling,Imageicon,我有类表情,它扩展了ImageIcon JLabel label = new JLabel("", emoticon); // emoticon is from class Emoticon 我不能这样做,因为JLabel正在寻找ImageIcon,而不是Emoticon。我能轻松解决这个问题吗 我已经扩展了ImageIcon,但我不确定是否应该这样做 public class Emoticon extends ImageIcon { private static final long ser

我有类表情,它扩展了ImageIcon

JLabel label = new JLabel("", emoticon); // emoticon is from class Emoticon
我不能这样做,因为JLabel正在寻找ImageIcon,而不是Emoticon。我能轻松解决这个问题吗

我已经扩展了ImageIcon,但我不确定是否应该这样做

public class Emoticon extends ImageIcon {
private static final long serialVersionUID = 1L;

Emoticon(String Path, String desc) {
    super(Path, desc);
}

Emoticon(Image image) {
    super(image);
}

public Emoticon getScaled(int width, int height) {
    Image image= this.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
    Emoticon ret = new Emoticon(image);
    return ret;

}

}
我稍后会扩展它

如何使表情符号与JLabel兼容?

我犯了一个错误

JLabel label = new JLabel("", emoticon);
应该是,也就是说

JLabel label = new JLabel("", emoticon, JLabel.CENTER);

谢谢Tom G你是什么意思,“[你]不能那样做?”当然可以。如果
Emoticon
是一个
ImageIcon
,那么它应该可以正常工作。您会准确地得到哪个错误?是的,如果
Emoticon
成为
ImageIcon
在逻辑上是合理的。您是否尝试过将
Emoticon
显式转换为
ImageIcon
<代码>新JLabel(“,(图像图标)表情符号,JLabel.CENTER)