简体   繁体   English

在另一个JPanel之上的JPanel

[英]JPanel on top of another JPanel

I have been using JPanels for a while and now want to place a JPanel on top of another JPanel. 我已经使用JPanels一段时间了,现在想将一个JPanel放置在另一个JPanel之上。

I have looked at using JLayer but I was wondering If there is a solution to just set the layer of the bottom and top, I don't want to set each components layer if possible. 我已经研究过使用JLayer,但是我想知道是否有解决方案可以只设置底部和顶部的层,所以我不想设置每个组件层。

Example

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

If this isn't possible what is the best solution for this, Thanks. 如果这不可能,那么最好的解决方案是什么,谢谢。

You can have the main panel use a BorderLayout . 您可以让主面板使用BorderLayout

Then you can do something like: 然后,您可以执行以下操作:

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

It sounds like what you want is a layout manager. 听起来您想要的是布局管理器。 There are a few different ones that suit different needs. 有几种不同的应用程序可以满足不同的需求。 There's a link at the bottom of this post. 这篇文章的底部有一个链接。

My personal favorite is GridLayout. 我个人最喜欢的是GridLayout。 So for what you want to do, you can do this: 因此,对于您想做什么,您可以执行以下操作:

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

That will do what you want. 那会做你想要的。

If you wanted to read more about them, here's a link: Oracle Docs on Layout Managers 如果您想了解有关它们的更多信息,请访问以下链接: 布局管理器上的Oracle文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM