在下面的代码中为什么结果是“paint!”这个字符出现了两次啊!不是说paint()这个方法在调用是只有调用一次吗?为什么下面的代码中它自动调用了paint方法两次?请高手解答啊 !!!
import java.awt.*;
import java.awt.event.*;public class testpaint2 extends Frame{
int x=50;public static void main(String[] args) {
testpaint2 t2=new testpaint2();
t2.launch();
}public void launch(){
this.setBackground(Color.GREEN);
this.setSize(800,600);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});}
public void paint(Graphics g){
System.out.println("paint!");
Color c=g.getColor();
g.setColor(Color.RED);
g.fillOval(x, 50, 30, 30);
System.out.println(x); System.out.println(x);
g.setColor(c);
x+=50;
}
}}
import java.awt.*;
import java.awt.event.*;public class testpaint2 extends Frame{
int x=50;public static void main(String[] args) {
testpaint2 t2=new testpaint2();
t2.launch();
}public void launch(){
this.setBackground(Color.GREEN);
this.setSize(800,600);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});}
public void paint(Graphics g){
System.out.println("paint!");
Color c=g.getColor();
g.setColor(Color.RED);
g.fillOval(x, 50, 30, 30);
System.out.println(x); System.out.println(x);
g.setColor(c);
x+=50;
}
}}
那么为什么setBackground(Color.GREEN)却不会在调一次paint(),而setSize(800,600)居然和paint()有了关系啊