有没有办法将 JPanel 放在 JPanel 上?

Is there a way to put JPanel on a JPanel?

我正在尝试将 JPanel 放在 JPanel 中或放在 JPanel 上,无论哪种情况,最终我只是希望它像这样工作

如图所示,红线是一个JFrame,里面有2个JPanel,绿色的JPanel上有一些不同的JPanel。

我需要有关绿色 JPanel 和其中的小 JPanel 的帮助。有没有办法让它像这样工作?

如有任何帮助,我们将不胜感激!

==============编辑 1==============

所以这里有一些代码,向您展示到目前为止我在@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);
    }
}

我以为上面是黄色的,下面的小部分是绿色的,结果全都变绿了。我做错了什么?

您需要使用 LayoutManager,这样当您将 JPanel 放在另一个 JPanel 中时,它会有正确的外观。如果您只是将面板放在其他面板中,则父 JPanel 将使用其默认布局管理器,即 FlowLayout.

从外观上看,您似乎需要使用 Border Layout for the parent (yellow) panel. For the green, you have options, but I think your best bet is to use Box LayoutPAGE_AXIS 组件方向。

一般来说,您需要熟悉两件事:1) 布局管理器及其行为方式,以及 2) JComponents 的默认布局行为。

您似乎想自己计算组件的位置和大小,并对 LayoutManager 执行其工作并调整大小和重新定位组件感到困惑。

所以您可以尝试关闭 LayoutManager 并自己进行定位 - 此处记录:https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

旁注: 您正在应用的结构是父 JPanel 内的 JPanel。 您还可以并排放置两个 JPanel——如果您只是将另一个 JPanel 子级添加到您的父级 JPanel,就会发生这种情况。如果一个组件有多个在屏幕位置相互重叠的子组件,则顺序对于渲染很重要(Z 顺序)。

在你的情况下,我会在 JFrame 的内容窗格中使用 BorderLayout,然后在中心区域添加游戏的主面板(鸡巴飞过 space),在南部地区。 绿色面板可以使用 FlowLayout 来简单地从左到右呈现它的子面板。

这会将您计算坐标的工作限制在游戏中,而不是 UI。

图中的 GUI 是使用三个面板创建的。

  1. YELLOW面板是游戏区。它没有布局,没有组件(定义它们自己的首选大小)并且是自定义绘制的,因此它定义了一个合理的首选大小以报告给布局管理器。
  2. GREEN 面板包含控件。它使用 FlowLayout.
  3. RED 面板使用 BorderLayout,并将 YELLOW 面板放在 CENTER 中,GREEN 面板放在 PAGE_END.

代码

这是制作上面截图的代码。

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);
    }
}