J2SE JPanel 空布局嵌套有关问题

J2SE JPanel 空布局嵌套问题

public static void main(String[] args){
  JPanel p1 = new JPanel();
  JPanel p2 = new JPanel();
  p2.add(p1);
  
p1.setLayout(null);
  JButton b = new JButton("asdfad");
  b.setBounds(0, 0, 20, 20);
  
p1.setBounds(0, 0, 100, 80);
  p1.add(b);  
  
  
p2.setLayout(null);  
  
  JFrame f = new JFrame();
  f.setBounds(0, 0, 200, 100);
  f.add(p2);
  f.setVisible(true);
 }

 

1、被添加面板为空布局

2、添加面板为默认,会出现组件消失

3、去掉设置空布局,组件出现

4、解决办法,在原面板设置位置,然后让添加面板布局也设置空就可以了

红字部分