Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么java.awt.Image不被视为java.awt.Component?_Java_Image_Swing_Inheritance_Compiler Errors - Fatal编程技术网

为什么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

扩展
对象
。即

  • 对象
    • 图像
A还扩展了
对象
。即

  • 对象
    • 组件
由于
组件
既不扩展
图像
图像
也不扩展
组件
,因此两者之间都没有关系。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) ) );

有关此概念的更多详细信息,请参阅本教程的“接口和继承”部分