网上截取的,感觉挺有用,记录下来。
http://bbs.csdn.net/topics/310041707
java swing 感觉好复杂啊…………一点都不想用但是作业要用到 >_<;
javax.swing.JComponent类中
repaint()调用update()
update()调用paint()
paint()调用paintComponent(),paintBorder(),及paintChildren()
附代码
1 package com.test; 2 import java.awt.Color; 3 import java.awt.Graphics; 4 5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7 8 public class GamePanel extends JPanel{ 9 10 private static final long serialVersionUID = 1L; 11 public GamePanel(){ 12 13 } 14 15 public void display(){ 16 System.out.println("GmaePanel - display"); 17 18 this.repaint(); 19 } 20 21 @Override 22 protected void paintComponent(Graphics g) { 23 24 System.out.println("GamePanel - ------------paintComponent"); 25 26 g.setColor(new Color(0xcfcfcf)); 27 } 28 29 @Override 30 public void paint(Graphics g) { 31 System.out.println("GamePanel - ------------paint"); 32 super.paint(g); 33 } 34 35 @Override 36 public void update(Graphics g) { 37 System.out.println("GamePanel - ------------update"); 38 super.update(g); 39 } 40 41 public static void main(String[] args){ 42 JFrame frame = new JFrame(); 43 frame.setSize(300,300); 44 GamePanel gp = new GamePanel(); 45 frame.add(gp); 46 frame.setVisible(true); 47 gp.display(); 48 } 49 }
时间: 2024-10-11 03:38:30