Swing重叠组件

Jef*_*rey 2 java layout swing

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

谢谢,杰夫

akf*_*akf 10

看看JLayeredPanes. 这是一个教程.

编辑:

如果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);
}   
Run Code Online (Sandbox Code Playgroud)