我在初始化 JPanel 时遇到问题。这是我的代码:`
JPanel canvas = new JPanel();
JPanel can = canvas;
System.out.println("COMPONENTS 1 :" + canvas.getComponentCount());
System.out.println("COMPONENTS 1 :" + can.getComponentCount());
JTextField txt = new JTextField();
txt.setBounds(100, 100, 200, 30);
can.add(txt);
System.out.println("COMPONENTS 2 :" + canvas.getComponentCount());
System.out.println("COMPONENTS 2 :" + can.getComponentCount());
`
输出是:
COMPONENTS 1 :0
COMPONENTS 1 :0
COMPONENTS 2 :1
COMPONENTS 2 :1
我想要的是 :
COMPONENTS 1 :0
COMPONENTS 1 :0
COMPONENTS 2 :0
COMPONENTS 2 :1