Method instance resolution for adding and setting JPanel in JFrame

Swing programs implement their windows with JFrame objects. The JFrame class is a subclass of the AWT frame class. It also adds some features unique to swing. It is very similar to the use of frame. The only difference is that you cannot add components to a JFrame. You can either add components to the content pane of JFrame or provide a new content pane.

The difference between panels and top-level containers: panels cannot exist independently and must be added to other containers (panels can be nested).

JFrame has a content pane in which all components that can be displayed in the window are added. JFrame provides two methods: getcontentpane and setcontentpane, which are used to get and set its content pane.   

There are two ways to add components to JFrame:

1) use the getcontentpane () method to obtain the content panel of JFrame, and then add the component: frame getContentPane (). add(childComponent)

2) create an intermediate container such as JPanel or jdesktopane, add components to the container, and set the container as the content panel of JFrame with setcontentpane() method:

Instance program:

Screenshot:

summary

The above is all about the method instance analysis of adding and setting JPanel in JFrame. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>