what's the use of a frame , a pane or a panel in swing?

前端 未结 1 672
青春惊慌失措
青春惊慌失措 2021-02-04 12:03

I read that JFrame is made of several panes ..what are panes and why is Jframe made of panes ? And why there is a JPanel while it seems that the JFrame looks exactly like the JP

相关标签:
1条回答
  • 2021-02-04 12:33

    There are top level containers such as JFrame. These can serve as the main window in which a GUI is built.

    Then there are intermediate level containers. These must be placed in other containers, they cannot exist by themselves. They either help you organize components or they add functionality. A JPanel is a very simple container that helps you to organize other components. While a JSplitPane adds the functionality of having two panes that are variable sized.

    When you have a complex GUI you may want to use JPanels to organize various areas of your GUI and then add each of the panels to your JFrame.

    In Java the Swing API makes use of the Composite Design Pattern. This means that you can compose very complex objects from other objects and still treat the composite objects the same way as the simple objects. So you can put a JPanel into a JPanel and it still behaves like a JPanel.

    Think of it like a tackle box (or sewing kit). It is made of a big container. But rather than put many small objects into this big container and make it difficult to manage later you can place some smaller compartments inside the big box. Then hooks and sinkers etc go in the compartments. Its easier to manage. The big box is the JFrame and the compartments are the JPanels.

    0 讨论(0)
提交回复
热议问题