java显示面板,Java Swing - 如何在另一个面板上显示面板?

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.

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.

Glass panes don't seem to be appropriate either.

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

EDIT

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.

解决方案

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

}

}

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

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值