swing 刷新问题
package scf.luffy;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Bell extends JFrame {
private int x1 = 100;
private int y1 = 100;
private int x2 = 100;
private int y2 = 100;
private static final long serialVersionUID = -8672941220924959402L;
Bell() {
setLayout(new GridLayout(1,1));
setSize(500, 500);
setLocation(100, 100);
setResizable(false);
setVisible(true);
Thread t = new GoGoGo();
SwingUtilities.updateComponentTreeUI(this);
t.start();
}
public void paint(Graphics g) {
/**/
Graphics2D g2d = (Graphics2D) g;
setBackground(Color.PINK);
g2d.setColor(Color.red);
g.drawRect(x1, y1, x2, y2);// 画矩形
}
class GoGoGo extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
x1 += 10;
x2 += 10;
y1 += 10;
y2 += 10;
System.out.println(x1);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
validate();
}
}
}
public static void main(String[] args) {
new Bell();
}
}
以上代码运行后 会画出许多 图形,但是实际上就是一个,没有刷新而已。
怎么让他能刷新啊~