为什么repaint没有调用paint或update?
[code=Java][/code]import java.awt.*;
import javax.swing.*;
public class Test implements Runnable{
private static BoardPanel boardPanel = new BoardPanel();
Test(){
new Thread(this).start();
}
@Override
public void run(){
int i = 1;
while(true){
try{
Thread.sleep(30);
}
catch(InterruptedException e){
}
boardPanel.repaint();
System.out.println("Thread run "+(i++));
}
}
public static void main(String[] args){
new Test();
BoardPanel boardPanel = new BoardPanel();
JFrame frame = new JFrame();
frame.add(boardPanel,BorderLayout.CENTER);
frame.setSize(475,494);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class BoardPanel extends JPanel{
@Override
public void update(Graphics g){
paint(g);
}
int i = 1;
@Override
public void paint(Graphics g){
System.out.println("paint "+(i++));
}
}
------解决方案--------------------
上边的解释有问题 应该是改变指向后boardPanel作为主线程的资源一直被占用了 所以另一个线程中才无法调用 大概应该是这个样子