9

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Patrick Reck
  • 11,246
  • 11
  • 53
  • 86

6 Answers6

23

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.

Community
  • 1
  • 1
Goaler444
  • 2,591
  • 6
  • 35
  • 53
  • Good explanation. I tried to run this example http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java but I got an error when `java FrameDemo`. Do you know if the code is right? – Sigur Nov 02 '16 at 13:19
  • Ow, I've just commented the `package` line and it works. – Sigur Nov 02 '16 at 13:20
1

10 different GUIs

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

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.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

A JFrame is a top-level component. It is a "skin" for your application. JPanels are not top-level containers. They need to reside inside of some other top-level container. JPanels can be added to the content pane of a JFrame.

I will address your question with a little sample code.

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.GridLayout;

public class Example extends JFrame implements Runnable
{
    JPanel left; 
    JPanel right;
    public Example()
    {
        super("Two JPanels in here");
        left = new JPanel(){
            public void paintComponent(Graphics g)
            {
                g.setColor(Color.red);
                g.fillRect(0,0,getWidth(), getHeight());
            }
        };
        right = new JPanel(){
            public void paintComponent(Graphics g)
            {
                g.setColor(Color.blue);
                g.fillRect(0,0,getWidth(), getHeight());
            }
        };

    }
    public void run()
    {
        setSize(500,500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(1,2));
        getContentPane().add(left);
        getContentPane().add(right);
        setVisible(true);
    }
    public static void main(String[] args)
    {
        Example e = new Example();
        javax.swing.SwingUtilities.invokeLater(e);
    }

}

Notice how the panels appear inside the content pane.

ncmathsadist
  • 4,684
  • 3
  • 29
  • 45
  • How will I approach doing this? – Patrick Reck Feb 13 '13 at 14:31
  • -1 for a paintComponent() example that doesn't invoke super.paintCompoent(...). While in this example it may not be required, some people will copy this code thinking it is never necessary. There is no reason to do custom painting anyway. If you want to change the background colour of the panel then just invoke panel.setBackground(...). – camickr Feb 13 '13 at 15:58
  • 1
    I am not animating or repeatedly repainting. Since the panels are essentially static (they color themselves their color), I did not call the parent method. – ncmathsadist Feb 13 '13 at 18:21
0

This article seems informative:

Summary:

  1. JPanel serves as a general purpose container, while JFrame is a window commonly used for stand-alone applications, like a warning window, or a notification window.

  2. JPanel represents an area used for more complex operations or applications.

  3. In JPanel, one panel can hold many operations, while in JFrame, it can have inner frames for a different purpose.

Howie
  • 2,760
  • 6
  • 32
  • 60
0

It does read as if a JFrame may be more flexible than a JPanel for a top-level window. As pointed out by @ncmathsadist, a JPanel may be embedded in a JFrame as well.

hd1
  • 33,938
  • 5
  • 80
  • 91
-1

Some of these answers are WAAAAAY too detailed.

Just use a JPanel.

You can always put a panel in a frame. You cant put a frame in a panel.

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225