问个关于Java中repaint()的问题:
调用display()时打印出GamePanel's display! 但不显示GamePanel's PrintComponent,也就是System.out.println("GamePanel's PrintComponent")这条语句没有执行,repaint()方法为什么不能调用 printComponent(Graphics g)方法。
- Java code
import java.awt.Graphics;import javax.swing.JPanel;public class RepaintDemo { public static void main(String args[]) { GamePanel gamepanel = new GamePanel(); gamepanel.display(); }}class GamePanel extends JPanel{ public void display() { System.out.println("GamePanel's display!"); this.repaint(); } protected void printComponent(Graphics g) { // TODO Auto-generated method stub System.out.println("GamePanel's PrintComponent"); } }
------解决方案--------------------
这个组件都没有显示, 怎么会调用printComponent?
------解决方案--------------------
repaint()的自动调用过程是这样的:repaint()-->update()-->paint()
而不会自动调用printComponent()