简体   繁体   中英

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

I am reading Core Java Volume 1 on Swing. 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. Figure 9.8 shows the inheritance hierarchy for 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. Moreover, JComponent is a subclass of Container, not Component. Therefore one can add other components into a JButton (However, those components would not be displayed).

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. JFrame and JDialog are Top Level Containers. If you closely look at the hierarchy, These top level containers does not inherit JComponent. Instead they inherit from AWT Window, which makes these top level containers are special in swing and make them as heavy weight component. 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. 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 ... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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