Using JFrame or JPanel

Patrick Reck picture Patrick Reck · Feb 13, 2013 · Viewed 21.5k times · Source

I'm about to begin developing a new small application. It'll contain around 10 different GUIs.

Using Netbeans, I can create either a JFrame or a JPanel - but which one should I use?

I'll have a Menu GUI which will contain buttons to the remaining GUIs. I will also have to pass some Controllers as parameters

Answer

Goaler444 picture Goaler444 · Feb 13, 2013

I assume you are a little bit confused on the differences between a JPanel and a JFrame. A JPanel acts as a container to hold other GUI components, such as buttons, textfields and other JPanels.

On the other hand, you can consider a JFrame to be a top-level component (the main JPanel).

Quotes and Images taken from this site and this site.

JFrame

A Frame is a top-level window with a title and a border.

The following is an image of a typical JFrame:

Typical JFrame

JPanel

The JPanel class provides general-purpose containers for lightweight components.

The following is an image of JPanels. Each colour represents a different JPanel

enter image description here:

Therefore, to answer your question, you need to use both. Start off with a JFrame (which acts as the main container), and add JPanels to build your graphical user interface.

I recommend that you read those two links to get a better idea and see the code.