class MyPanel extends JPanel{
Rectangle bianjie = null;
public void paint(Graphics g){
super.paint(g);
bianjie = this.getBounds();
this.setBackground(Color.black);
System.out.print(bianjie.width);
}
}
bianjie = this.getBounds();这一句放在paint函数中会输出正确的width;
如何放在构造函数的话如下
class MyPanel extends JPanel{
Rectangle bianjie = null;
public void paint(Graphics g){
super.paint(g);
this.setBackground(Color.black);
System.out.print(bianjie.width);
}
MyPanel(){
bianjie = this.getBounds();
}
}
就会输出0
为什么
Rectangle bianjie = null;
public void paint(Graphics g){
super.paint(g);
bianjie = this.getBounds();
this.setBackground(Color.black);
System.out.print(bianjie.width);
}
}
bianjie = this.getBounds();这一句放在paint函数中会输出正确的width;
如何放在构造函数的话如下
class MyPanel extends JPanel{
Rectangle bianjie = null;
public void paint(Graphics g){
super.paint(g);
this.setBackground(Color.black);
System.out.print(bianjie.width);
}
MyPanel(){
bianjie = this.getBounds();
}
}
就会输出0
为什么