當我運行此代碼時,除了空白(白色)面板外,我什麼都看不到,我想知道爲什麼。爲什麼我的while循環在paintComponent中不起作用?
這裏是我的代碼:
Graph.java
public class Graph extends JPanel {
private static final long serialVersionUID = -397959590385297067L;
int screen=-1;
int x=10;
int y=10;
int dx=1;
int dy=1;
boolean shouldrun=true;
imageStream imget=new imageStream();
protected void Loader(Graphics g){
g.setColor(Color.black);
g.fillRect(0,0,x,y);
x=x+1;
y=y+2;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
while(shouldrun){
Loader(g);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
看一看[Swing中的併發(http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)和[如何使用Swing定時器(http://docs.oracle.com /javase/tutorial/uiswing/misc/timer.html) – MadProgrammer