java Swing 重叠组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1428193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:31:28  来源:igfitidea点击:

Swing Overlapping components

javaswinglayout

提问by Jeff Storey

I have two AWT components in a Frame, Panel A and Panel B. I would like panel A to be sized to the height width of the frame (and maintain that size on frame resize), but I would like panel B to overlap A. B will be at a fixed position (0,0 to make it easier) with a fixed height and width. I'm not sure what kind of layout manager I would need to make this work. If I use a null layout, I think I would have to manage the resizing of panel A myself, but it would make the sizing of panel B relatively easy. Any thoughts on how to accomplish this?

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

thanks, Jeff

谢谢,杰夫

回答by akf

Take a look at JLayeredPanes. Here is a tutorial.

看看JLayeredPanes这是一个教程

edit:

编辑:

If panelA is an AWT component, you will be hard pressed to get panelB to overlap. From Sun's article entitled Mixing Heavy and Light Components:

如果 panelA 是 AWT 组件,您将很难让 panelB 重叠。来自 Sun 题为Mixing Heavy and Light Components的文章:

Do not mix lightweight (Swing) and heavyweight (AWT) components within a container where the lightweight component is expected to overlap the heavyweight one.

不要在容器中混合轻量级 (Swing) 和重量级 (AWT) 组件,其中轻量级组件预计会与重量级组件重叠。

However, if you are looking to have panelA fill the Frame completely, why not add panelB as a component of panelA?

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

Edit2:

编辑2:

If you can make panelB a heavyweight component, then you can use the JLayeredPane.

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

Here is a quick mockup that shows how:

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

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

回答by Reverend Gonzo

Your best bet is to have your own LayoutManager. The easiest way is probably to extend or proxy BorderLayout, and have a specific case to layout panel B.

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

回答by CodePartizan

Maybe I'm missing something. If B is fixed size and is at (0,0) and A runs the full width what's the use of having B overlap A? You will never see anything that is placed under B.

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

I can't think of any of the default layout managers, but Orielly has this one you can use: relative layout manager(see source code link) with documentation. I haven't used it in a long while, but it should beat managing the layout yourself.

我想不出任何默认的布局管理器,但 Orielly 有一个你可以使用的: relative layout manager(see source code link) with documentation。我有很长一段时间没有使用它了,但它应该胜过自己管理布局。