为什么java.awt.Image不被视为java.awt.Component?
我试图在组件和图像之间切换面板的内容, 它适用于以下组件:为什么java.awt.Image不被视为java.awt.Component?,java,image,swing,inheritance,compiler-errors,Java,Image,Swing,Inheritance,Compiler Errors,我试图在组件和图像之间切换面板的内容, 它适用于以下组件: imgpanel.removeAll(); Component comp; if ((comp = player.getVisualComponent()) != null) { imgpanel.add(comp); } 不适用于图像: btoi = new BufferToImage((VideoFormat) buf.getFormat()); img = btoi.createImage(buf); imgpanel.
imgpanel.removeAll();
Component comp;
if ((comp = player.getVisualComponent()) != null) {
imgpanel.add(comp);
}
不适用于图像:
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf);
imgpanel.removeAll();
imgpanel.add(img);//The method add(Component) in the type Container is not applicable for the arguments (Image)
我在这里该怎么办
为什么java.awt.Image
不被视为java.awt.Component
扩展对象
。即
对象
图像
对象
。即
对象
组件
组件
既不扩展图像
,图像
也不扩展组件
,因此两者之间都没有关系。OTOH两者都有仅与对象
存在关系。即,图像
是一个对象
&组件
是一个对象
我在这里该怎么办
在设计用于显示图像的组件中显示图像,例如JLabel中的ImageIcon
。所以它可能看起来像这样:
panel.add( new JLabel( new ImageIcon(image) ) );
有关此概念的更多详细信息,请参阅本教程的“接口和继承”部分
为什么java.awt.Image
不被视为java.awt.Component
扩展对象
。即
对象
图像
A还扩展了对象
。即
对象
组件
由于组件
既不扩展图像
,图像
也不扩展组件
,因此两者之间都没有关系。OTOH两者都有仅与对象
存在关系。即,图像
是一个对象
&组件
是一个对象
我在这里该怎么办
在设计用于显示图像的组件中显示图像,例如JLabel中的ImageIcon
。所以它可能看起来像这样:
panel.add( new JLabel( new ImageIcon(image) ) );
有关此概念的更多详细信息,请参阅本教程的“接口和继承”部分