Swing重叠组件

我在框架,面板A和面板B中有两个AWT组件。我希望面板A的大小适合框架的高度宽度(并保持框架大小的大小),但我希望面板B重叠A. B将处于固定位置(0,0以使其更容易),具有固定的高度和宽度。 我不确定我需要什么样的布局管理器来完成这项工作。 如果我使用null布局,我想我必须自己管理面板A的大小调整,但它会使面板B的大小相对容易。 有关如何实现这一点的任何想法?

谢谢,杰夫

看看JLayeredPane的 。 这是一个教程 。

编辑:

如果panelA是AWT组件,则很难让panelB重叠。 来自Sun的文章“ 混合重型和轻型部件 :

不要在容器内混合轻量级(Swing)和重量级(AWT)组件,轻量级组件应与重量级组件重叠。

但是,如果您希望panelA完全填充Frame,为什么不将panelB添加为panelA的组件?

EDIT2:

如果您可以使panelB成为重量级组件,那么您可以使用JLayeredPane

这是一个快速模型,显示如何:

 public static void main(String[] args){ new GUITest(); } public GUITest() { frame = new JFrame("test"); frame.setSize(300,300); addStuffToFrame(); SwingUtilities.invokeLater(new Runnable(){ public void run() { frame.setVisible(true); } }); } private void addStuffToFrame() { Panel awtPanel = new Panel(); awtPanel.setBackground(Color.blue); //here you can fool around with the pane: //first, you can see how the layered pane works by switching the //DEFUALT_LAYER and PALLETTE_LAYER back and forth between the two panels //and re-compiling to see the results awtPanel.setSize(200,300); frame.getLayeredPane().add(awtPanel, JLayeredPane.DEFAULT_LAYER); //next you comment out the above two lines and //uncomment the following line. this will give you the desired effect of //awtPanel filling in the entire frame, even on a resize. //frame.add(awtPanel); Panel awtPanel2 = new Panel(); awtPanel2.setBackground(Color.red); awtPanel2.setSize(300,200); frame.getLayeredPane().add(awtPanel2,JLayeredPane.PALETTE_LAYER); } 

最好的办法是拥有自己的LayoutManager。 最简单的方法可能是扩展或代理BorderLayout,并具有布局面板B的特定情况。

也许我错过了一些东西。 如果B是固定大小并且在(0,0)并且A运行全宽,那么使用B重叠A是什么用途? 你永远不会看到B下的任何东西。

我想不出任何默认的布局管理器,但是Orielly可以使用这个: 相对布局管理器 (参见源代码链接)和文档 。 我很久没有使用它,但它应该自己管理布局。