Is there a way to put JPanel on a JPanel?

Viewed 199

I'm trying to put a JPanel inside OR on a JPanel, whichever may be the case, ultimately I just want this to work like this Insanely well painted description of the problem

As you can see on the picture, the red line is a JFrame and it has 2 JPanels inside it, on the green JPanel there are some different JPanels.

I need help with the green JPanel and the little JPanels inside it. Is there any way to make it work like this?

Any help would be greatly appreciated!

==============EDIT 1==============

So here is some code, to show you what I've done so far with the help of @hfontanez.

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class Main
{
    public static void main(String[] args)
    {
        //JFrame
        JFrame jframe = new JFrame();
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setSize(1920, 1080);
        jframe.setResizable(false);
        jframe.setLocationRelativeTo(null);
        jframe.setVisible(true);

        //parentJpanel   -   This is the main panel
        JPanel parentJpanel = new JPanel();
        parentJpanel.setBackground(Color.YELLOW);
        parentJpanel.setSize(1920, 1080);
        parentJpanel.setLayout(new BorderLayout());

        //smallPanel    -   This is the little panel on the bottom
        JPanel smallPanel = new JPanel();
        smallPanel.setBackground(Color.GREEN);
        smallPanel.setSize(1920, 300);
        smallPanel.setLocation(0, 780);
        smallPanel.setLayout(new BoxLayout(smallPanel, BoxLayout.PAGE_AXIS));

        parentJpanel.add(smallPanel);
        jframe.add(parentJpanel);
    }
}

The result of the code below

I expected the top part to be yellow, and the small part on the bottom to be green, yet the whoel thing turned green. What did I do wrong?

2 Answers

enter image description here

The pictured GUI is created using three panels.

  1. The YELLOW panel is the game play area. It has no layout, no components (which define their own preferred sizes) and is custom painted, so it defines a sensible preferred size to report to the layout manager.
  2. The GREEN panel contains controls. It uses a FlowLayout.
  3. The RED panel uses a BorderLayout, and puts the YELLOW panel in the CENTER and the GREEN panel in the PAGE_END.

Code

This is the code that made the screenshot seen above.

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class GameLayout {

    GameLayout() {
        // The main GUI. Everything else is added to this panel
        JPanel gui = new JPanel(new BorderLayout(5, 5));
        gui.setBorder(new EmptyBorder(4, 4, 4, 4));
        gui.setBackground(Color.RED);

        // The custom painted area - it is a panel that defines its preferred size.
        gui.add(new GamePanel());

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        buttonPanel.setBackground(Color.GREEN);
        for (int ii = 1; ii<5; ii++) {
            buttonPanel.add(new JButton("B " + ii));
        }
        gui.add(buttonPanel,BorderLayout.PAGE_END);

        JFrame f = new JFrame("Game Layout");
        f.setContentPane(gui);
        f.setLocationByPlatform(true);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = () -> new GameLayout();
        SwingUtilities.invokeLater(r);
    }
}

class GamePanel extends JPanel {
    GamePanel() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 100);
    }
}

You need to use a LayoutManager so when you put the JPanel inside the other JPanel it will have the correct look. If you simply put panels inside the others, the parent JPanel will use its default layout manager, which is FlowLayout.

For the look of it, it seems you need to use Border Layout for the parent (yellow) panel. For the green, you have options, but I think your best bet is to use Box Layout with a PAGE_AXIS Component Orientation.

In general, you need to be familiarized with two things: 1) Layout Managers and how they behave, and 2) the default layout behavior of JComponents.

Related