Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JFrame or JPanel

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

like image 861
Patrick Reck Avatar asked Feb 13 '13 14:02

Patrick Reck


People also ask

Do I need JFrame?

The JFrame Class. Whenever you create a graphical user interface with Java Swing functionality, you will need a container for your application. In the case of Swing, this container is called a JFrame. All GUI applications require a JFrame.

Why do we use JFrame?

JFrame is a top-level container that provides a window on the screen. A frame is actually a base window on which other components rely, namely the menu bar, panels, labels, text fields, buttons, etc. Almost every other Swing application starts with the JFrame window.

Why do we use JPanel?

JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar.

Is Swing outdated Java?

JavaFX new fixes will continue to be supported on Java SE 8 through March 2022 and removed from Java SE 11. Swing and AWT will continue to be supported on Java SE 8 through at least March 2025, and on Java SE 11 (18.9 LTS) through at least September 2026.


2 Answers

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.

like image 170
Goaler444 Avatar answered Sep 25 '22 19:09

Goaler444


10 different GUIs

There are a variety of option for displaying the JPanel based GUI elements.

  • CardLayout
  • JTabbedPane
  • Nested layout..

See The Use of Multiple JFrames, Good/Bad Practice? for many other ideas about combining or displaying different groups of widgets.

N.B. Resist extending either JFrame or JPanel - instead simply keep a reference to one, and use it as needed.

like image 41
Andrew Thompson Avatar answered Sep 26 '22 19:09

Andrew Thompson