public class SwingTest extends JFrame{
JPanel jp1,jp2;
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingTest mp = new SwingTest();
}
public SwingTest(){
jp1 = new JPanel();
jp2 = new JPanel();
jp1.setBackground(Color.blue);
jp2.setBackground(Color.yellow);
System.out.println(jp2.getHeight()+" "+jp2.getWidth());
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.CENTER);
this.setVisible(true);
this.setExtendedState( JFrame.MAXIMIZED_BOTH );
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出是 0 0 ????这是为什么
------解决方案--------------------
你把System.out.println(jp2.getHeight()+" "+jp2.getWidth());放在setVisible(true)后面就能取到值了。没有显示之前,panel大小为0