Java Swing - 如何在另一个面板上显示面板? [英] Java Swing - how to show a panel on top of another panel?

查看:3201
本文介绍了Java Swing - 如何在另一个面板上显示面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个内部(非窗口)对话框要求成员输入。我希望将对话框集中放在现有的JPanel上。

I wish to have an internal (non window) dialog to ask for member input. I would like the dialog to be placed centrally on an existing JPanel.

我已经看过 layeredpanes ,由于在所有窗格中只有一个布局管理器(或没有布局管理器),这些似乎不可用。我想我可以尝试覆盖JLayeredPane并提供自定义的布局,但这似乎是极端的。

I have looked at layeredpanes and these seem unusable due to only having a single layout manager (or no layout manager) across all the panes. I guess I could try to override JLayeredPane and provide a custom layout but this seems extreme.

玻璃窗格似乎也不合适。

如何这样做?在Swing中没有z-index的可用概念?

How can this be done? Is there no usable concept of z-indexes in Swing?

http://i42.tinypic.com/30tkuvk.jpg

编辑

分层窗格不合适的原因是由于每层缺少布局管理器。面板可调整大小,面板A应保持在100%的面积,面板B应保持集中。

The reason Layered Panes weren't appropriate was due to the lack of a layout manager per layer. The panel is resizeable, Panel A should stay at 100% of area and Panel B should stay centralized.

推荐答案

我认为LayeredPane是你最好打赌这里。您将需要第三个面板,以包含A和B.此第三个面板将是分层面板,然后面板A和B仍然可以有一个漂亮的LayoutManagers。所有你需要做的是中心B在A上,并有相当多的例子在Swing的轨迹上如何做到这一点。 没有布局管理器的定位教程

I think LayeredPane is your best bet here. You would need a third panel though to contain A and B. This third panel would be the layeredPane and then panel A and B could still have a nice LayoutManagers. All you would have to do is center B over A and there is quite a lot of examples in the Swing trail on how to do this. Tutorial for positioning without a LayoutManager.

public class Main {
    private JFrame frame = new JFrame();
    private JLayeredPane lpane = new JLayeredPane();
    private JPanel panelBlue = new JPanel();
    private JPanel panelGreen = new JPanel();
    public Main()
    {
        frame.setPreferredSize(new Dimension(600, 400));
        frame.setLayout(new BorderLayout());
        frame.add(lpane, BorderLayout.CENTER);
        lpane.setBounds(0, 0, 600, 400);
        panelBlue.setBackground(Color.BLUE);
        panelBlue.setBounds(0, 0, 600, 400);
        panelBlue.setOpaque(true);
        panelGreen.setBackground(Color.GREEN);
        panelGreen.setBounds(200, 100, 100, 100);
        panelGreen.setOpaque(true);
        lpane.add(panelBlue, new Integer(0), 0);
        lpane.add(panelGreen, new Integer(1), 0);
        frame.pack();
        frame.setVisible(true);
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main();
    }

}

您可以使用setBounds来定位面板

You use setBounds to position the panels inside the layered pane and also to set their sizes.

编辑以反映原始帖子的更改
您将需要添加组件检测父容器正在调整大小的侦听器,然后动态更改面板A和B的边界。

Edit to reflect changes to original post You will need to add component listeners that detect when the parent container is being resized and then dynamically change the bounds of panel A and B.

这篇关于Java Swing - 如何在另一个面板上显示面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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