gpt4 book ai didi

java - 将 JLayeredPane 添加到 JPanel

转载 作者:行者123 更新时间:2023-11-30 03:28:53 25 4
gpt4 key购买 nike

我正在尝试将 JLayeredPane 插入到 JPanel 中。当我将 JLayeredPane 插入框架时,组件显示并且一切正常,但是当我将 JLayeredPane 插入 JPanel,然后将 JPanel 插入框架时,它不显示 JLayeredPane。

我的代码:

button1.setBounds(0, 0, 100, 100);
button2.setBounds(50, 50, 100, 100);
layer.add(button1,1);
layer.add(button2,0);
panel.add(layer);
this.add(panel);
this.setVisible(true);

最佳答案

I insert the JLayeredPane to the JPanel and then insert the JPanel to the frame It doesn't display the JLayeredPane.

JPanel 使用 FlowLayout,它尊重添加到其中的任何组件的首选大小。

JLayeredPane 不使用布局管理器,因此没有首选大小,因此不会显示在面板上。

the components show and everything is okay,

使用 BorderLayout 的框架。默认位置是“中心”,中心将根据框架中的可用空间调整分层 Pane 的大小。

不需要将分层 Pane 添加到面板,然后将面板添加到框架的额外开销。

阅读 Swing 教程中关于 How to Use Layered Panes 的部分了解更多信息和工作示例。

关于java - 将 JLayeredPane 添加到 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29542812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com