简体   繁体   English

澄清Java Swing中组件类的继承层次结构?

[英]clarification on inheritance hierarchy for the component class in Java Swing?

I am reading Core Java Volume 1 on Swing. 我正在Swing上阅读Core Java Volume 1 I am having difficulty understanding this paragraph: Firstly, the author says 我很难理解这一段:首先,作者说

Components can be placed inside containers such as panels. 组件可以放置在诸如面板之类的容器内。 Container can themselves be put inside other containers,so the class container extends Component. 容器本身可以放在其他容器中,因此类容器扩展了Component。 Figure 9.8 shows the inheritance hierarchy for Component, 图9.8显示了Component的继承层次结构,

在此处输入图片说明

Note: Unfortunately, the inheritance hierarchy is somewhat unclean in two respects. 注意:不幸的是,继承层次结构在两个方面有些不干净。 First,top-level windows, such as JFrame, are subclasses of container and hence Component, but they cannot be placed inside other containers. 首先,顶级窗口(例如JFrame)是容器的子类,因此是Component的子类,但是它们不能放置在其他容器中。 Moreover, JComponent is a subclass of Container, not Component. 而且,JComponent是Container的子类,而不是Component。 Therefore one can add other components into a JButton (However, those components would not be displayed). 因此,可以将其他组件添加到JButton中(但是,这些组件将不会显示)。

To me, both his statements before and after the picture seems contrary. 在我看来,他在照片前后的说法似乎是相反的。 I also could not make a clear sense of what the author is trying to say here. 我也不清楚作者在这里要说些什么。 if someone could elaborate with an example or something, that will be useful Thanks 如果有人可以举例说明某事,那将很有用,谢谢

As @MadProgrammer said in comment, author is highlighting a discrepancy between what the object hierarchy is telling you and what the API can do. 正如@MadProgrammer在评论中所说,作者强调了对象层次结构告诉您的内容与API可以做什么之间的差异。 JFrame and JDialog are Top Level Containers. JFrame和JDialog是顶级容器。 If you closely look at the hierarchy, These top level containers does not inherit JComponent. 如果仔细查看层次结构,这些顶级容器不会继承JComponent。 Instead they inherit from AWT Window, which makes these top level containers are special in swing and make them as heavy weight component. 相反,它们是从AWT窗口继承的,这使这些顶级容器在摆动过程中很特殊,并使其成为重量级的组件。 So these heavy weight top level container can not be placed inside another container or component. 因此,这些重量较重的顶层容器不能放置在其他容器或组件内。 All the components that extends JComponents are light weight components. 扩展JComponents的所有组件都是轻量级的组件。 At this point,all light weight components can be placed inside another light weight components or top level containers. 此时,可以将所有轻量组件放置在另一个轻量组件或顶层容器中。

Let me know if i went wrong ... :) 让我知道我是否出错... :)

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

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