Java JPanel位于另一个JPanel之上

Java JPanel位于另一个JPanel之上,java,swing,jpanel,jlayer,Java,Swing,Jpanel,Jlayer,我已经使用JPanel一段时间了,现在我想把一个JPanel放在另一个JPanel的上面 我已经研究过使用JLayer,但我想知道是否有一种解决方案可以只设置底部和顶部的层,如果可能的话,我不想设置每个组件层 示例 JPanel bottomPanel = new JPanel(); # Set as bottom panel JPanel topPanel = new JPanel(); # Set as top panel JPanel sidePanel = ne

我已经使用JPanel一段时间了,现在我想把一个JPanel放在另一个JPanel的上面

我已经研究过使用JLayer,但我想知道是否有一种解决方案可以只设置底部和顶部的层,如果可能的话,我不想设置每个组件层

示例

JPanel bottomPanel = new JPanel();      # Set as bottom panel
JPanel topPanel = new JPanel();         # Set as top panel
JPanel sidePanel = new JPanel();        # Don't have to set
JPanel anotherSidePanel = new JPanel(); # Don't have to set

如果这是不可能的,什么是最好的解决方案,谢谢。

您可以让主面板使用
边框布局

然后,您可以执行以下操作:

mainPanel.add(leftSide, BorderLayout.LINE_START);
mainPanel.add(rightSide, BorderLayout.LINE_END);
JLayeredPane lp = new JLayeredPane();
mainPanel.add(lp, BorderLayout.CENTER);

您可以让主面板使用
边框布局

然后,您可以执行以下操作:

mainPanel.add(leftSide, BorderLayout.LINE_START);
mainPanel.add(rightSide, BorderLayout.LINE_END);
JLayeredPane lp = new JLayeredPane();
mainPanel.add(lp, BorderLayout.CENTER);

听起来你想要的是一个布局管理器。有几种不同的方法可以满足不同的需求。这篇文章的底部有一个链接

我个人最喜欢的是GridLayout。因此,对于您想要做的事情,您可以这样做:

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
//the first number is the number of rows, the second is the number of columns

JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
panel.add(topPanel);
panel.add(bottomPanel);
那会做你想做的

如果您想了解更多关于他们的信息,请点击以下链接:

听起来你想要的是一个布局管理器。有几种不同的方法可以满足不同的需求。这篇文章的底部有一个链接

我个人最喜欢的是GridLayout。因此,对于您想要做的事情,您可以这样做:

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
//the first number is the number of rows, the second is the number of columns

JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
panel.add(topPanel);
panel.add(bottomPanel);
那会做你想做的

如果您想了解更多关于他们的信息,请点击以下链接:

以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则具有更大的宽度和高度。以最小尺寸提供GUI预期布局的ASCII艺术或简单绘图,如果可以调整大小,则具有更大的宽度和高度。哇,从来都不知道有
JLayeredPane
,谢谢,成功了!哇,我从来都不知道有这样的东西,
JLayeredPane
,谢谢,它起作用了!