What is the use of a frame, panel or panel in a swing?

I read that JFrame consists of several panels. What are panels and why is Jframe made of panels? And why does JPanel exist, while it seems that the JFrame looks exactly like JPanel, but with the menu bar and close button, what is needed for JPanel? Can someone explain to me the clear definition and use of these three components?

+8
java swing
source share
1 answer

There are top-level containers like JFrame. They can serve as the main window in which the graphical interface is built.

Then there are intermediate tier containers. They must be placed in other containers; they cannot exist on their own. They either help you organize components or add functionality. JPanel is a very simple container that helps you organize other components. While JSplitPane adds the functionality of having two panels with a variable size.

When you have a sophisticated GUI, you can use JPanels to organize different areas of your GUI, and then add each of the panels to your JFrame.

The Java Swing API uses a Composite Design Template . This means that you can create very complex objects from other objects and process compound objects in the same way as simple objects. That way you can put the JPanel in the JPanel, and it still behaves like a JPanel.

Think of it as a tool box (or sewing kit). It is made of a large container. But instead of putting a lot of small items in this large container and making it difficult to manage, you can place several small compartments inside a large box. Then hooks and sinkers, etc. Enter the compartments. Its easier to manage. The big box is JFrame and the compartments are JPanels.

+15
source share

Source: https://habr.com/ru/post/650271/


All Articles