且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GUI中重叠的JPanels

更新时间:2023-12-05 22:44:52

我想使censingPanel透明并且使getGraphPaneScrollPane在GUI中高于它,

I want situationPanel to be transparent and getGraphPaneScrollPane to be above that in the GUI,

最上面的面板是需要透明的面板.如果顶部面板是不透明的,那么您将永远不会在顶部面板下方看到该面板.

The panel that is on top is the panel that needs to be transparent. If the panel on top is opaque then you will never see the panel under the top panel.

所以最后要更改布局是我想要的.

So making changes to the layout is the last thing I want.

这就是您需要做的.您不能只将两个面板添加到一个面板中,并期望它按照您希望的方式工作.大多数Swing布局管理器旨在将组件二维放置,而不是彼此重叠.

Well that is what you are going to need to do. You can't just add two panels to one panel and expect it to work the way you want it to. Most Swing layout managers are designed to lay out components in two dimensions, not on top of one another.

您当前的代码是:

mainPanel.setLayout(new BorderLayout());
mainPanel.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.add(getSituationPanel(), BorderLayout.CENTER);

您可以尝试使用OverlayLayout,它旨在将面板布置在另一个之上.该代码应类似于:

You could try using the OverlayLayout, it is designed to lay out panels on top of on another. The code should be something like:

JPanel overlay = new JPanel();
overlay.setLayout( new OverlayLayout(overlay) );
overlay.add(getSituationPanel(), BorderLayout.CENTER); // add transparent panel first
overlay.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.setLayout(new BorderLayout()); 
mainPanel.add(overlay);