摆动重叠组件 [英] Swing Overlapping components

查看:62
本文介绍了摆动重叠组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在框架中有两个AWT组件:面板A和面板B.我希望将面板A调整为框架的高度宽度(并在调整框架大小时保持该大小),但是我希望面板B调整为重叠A.B将位于固定高度(宽度为0,0),固定高度和宽度.我不确定要进行此工作需要哪种布局管理器.如果我使用空布局,我想我必须自己管理面板A的大小,但这会使面板B的大小相对容易.关于如何实现这一目标有什么想法吗?

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?

谢谢, 杰夫

推荐答案

看看这是一个教程.

如果panelA是AWT组件,则将很难使panelB重叠.摘自Sun题为混合重型和轻型零部件

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:

请勿在容器中混合轻质(Swing)组件和重量级(AWT)组件,否则容器应与轻质组件重叠.

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

但是,如果您希望将panelA完全填满框架,为什么不将panelB添加为panelA的组成部分?

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

Edit2:

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

If you can make panelB a heavyweight component, then you can use the 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);
}   

这篇关于摆动重叠组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆