I've trying to get two JPanels to layer on top of one another, but at the same time, expand to fit the size of their parent. For example, if the JFrame that contains the two layered JPanels is expanded, the layered JPanels also expand to fill the entire JFrame, and vice-versa when the JFrame is collapsed.

The idea is to create a top layer that contains buttons and other Swing components that control a bottom layer, but the bottom layer can still be seen through the top layer (by setting the top layer's properties to setOpaque(false)). At the same time, both the top and the bottom layers should fill the available size of their container (the JPanel or JFrame that contains the layers).

I have already tried working with JLayeredPane, but the size of the pane must be known in advanced (as far I have been able to get it working so far). These size constraints will change as the size of the containing JPanel or JFrame change.

Thank you in advanced for your help.

有帮助吗?

解决方案

You Could...

You try using a OverlayLayout, but I'm not sure it will meet your requirements.

You Could

Set use a GridBagLayout on the JLayeredPane, placing each component at the same gridx/gridy and then filling with GridBagConstraints.BOTH and a weightx/weighty of 1

You Could

Use the frame's "glass pane" as the top layer. Which will allow you to place controls over the top the frame's "content pane"

See How to use root panes for more details.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top