Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

swing

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 JPanel but with a menu bar and a close button so what's the need for a JPanel ? Can anybody explain to me clearly the definition and use of those 3 components ?

like image 780
Ahmed Avatar asked Dec 18 '10 18:12

Ahmed


1 Answers

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.

like image 137
Vincent Ramdhanie Avatar answered Nov 08 '22 12:11

Vincent Ramdhanie