将 JLayeredPane 添加到 JPanel
Adding a JLayeredPane to a JPanel
我正在尝试将 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,
使用边框布局的框架。默认位置是 "CENTER",中心将根据框架中可用的 space 调整分层窗格的大小。
不需要额外的开销将分层窗格添加到面板,然后将面板添加到框架。
阅读有关 How to Use Layered Panes 的 Swing 教程部分,了解更多信息和工作示例。
我正在尝试将 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,
使用边框布局的框架。默认位置是 "CENTER",中心将根据框架中可用的 space 调整分层窗格的大小。
不需要额外的开销将分层窗格添加到面板,然后将面板添加到框架。
阅读有关 How to Use Layered Panes 的 Swing 教程部分,了解更多信息和工作示例。