简体   繁体   English

为什么不将java.awt.Image视为java.awt.Component?

[英]Why java.awt.Image is not considered as java.awt.Component?

I was trying to switch the content of Panel between Component and Image, It works for the Component: 我试图在组件和图像之间切换面板的内容,它适用于组件:

imgpanel.removeAll();
Component comp;
if ((comp = player.getVisualComponent()) != null) {
    imgpanel.add(comp);
}

Not for the Image: 不适用于图片:

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)

What should I do here? 我该怎么办?

Why java.awt.Image is not considered as java.awt.Component ? 为什么java.awt.Image不被视为java.awt.Component

An Image extends Object . Image扩展Object IE IE浏览器

  • Object
    • Image

A Component also extends Object . Component还扩展了Object IE IE浏览器

  • Object
    • Component

Since neither Component extends Image , nor Image extends Component , neither has an is an relationship to the other. 由于既没有Component扩展Image ,也没有Image扩展Component ,所以两者都不存在彼此之间的关系。 OTOH both have the is an relationship only with Object . OTOH两者仅与Object 关系。 IE Image is an Object & Component is an Object . IE Image Object ,而Component Object

What should I do here? 我该怎么办?

Display the image in a Component designed to display images, such as (an ImageIcon in) a JLabel . 在用于显示图像的Component中显示图像,例如JLabel (中的ImageIcon )。 So it might look like this: 因此可能看起来像这样:

panel.add( new JLabel( new ImageIcon(image) ) );

For more details on this concept, see Inheritance in the 'Interfaces and Inheritance' section of the tutorial. 对于此概念的更多详细信息,请参阅继承教程的“接口和继承”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM