日期:2014-05-20  浏览次数:20789 次

java 中public void paint()方法问题
import java.awt.*;
import java.awt.event.*;

class MyBtton extends Button implements ActionListener
{ int x=10,y=10,i=0;
  Color color[]={Color.red,Color.yellow,Color.green};
  Color c=color[0];
  MyBtton()
  { setSize(38,85);
  setBackground(Color.cyan);
  addActionListener(this);  
 
  }
  public void paint(Graphics g)
  { g.setColor(c);
g.drawOval(x, y, 20, 20);
 
 }
  public void update(Graphics g) 
 { g.clearRect(x, y, 20, 20);
//paint(g) ;
}
  public void actionPerformed(ActionEvent e)
  { i=(i+1)%3;
  c=color[i];
  y=y+23;
  if(y>56)
  y=10;
  repaint();
   
  }
}
class WindowCanvas extends Frame
{ WindowCanvas()
{ MyBtton button=new MyBtton();
setLayout(null);
add(button);
button.setLocation(30,30);
setBounds(60,125,100,200);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  System.exit(0);
  }
  });
}
}
public class PaintExa {

/**
* @param args
*/
public static void main(String[] args) {
new WindowCanvas();
// TODO Auto-generated method stub

}

}


上面的程序是实现交通指示灯的样式public void update(Graphics g) 
 { g.clearRect(x, y, 20, 20);
paint(g) ;
}

 repaint();
我把这三句删掉,程序任能实现该功能,这是因为事件处理之后,自动调用父类的方法吗?还有public void paint()重写父类方法怎么不调用就能运行??

------解决方案--------------------
多态基础
请翻书...