import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Client extends Frame {
int x = 50; int y = 50; @Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
y += 5;
g.setColor(c); } public void launchFrame() {
this.setLocation(400, 300);
this.setSize(800, 600);
this.setTitle("TankWar");
this.setResizable(false);
this.setVisible(true);
this.setBackground(Color.GREEN);
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
new Thread(new ThreadM()).start();
} public static void main(String[] args) {
new Client().launchFrame(); } private class ThreadM implements Runnable { public void run() { while (true) {
new Client().repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} } }} 
 为啥这样写不能重画出来那个圆阿?谢谢!

解决方案 »

  1.   

    把new Client().repaint();
    改成repaint()
    就可以了
    为什么阿?
      

  2.   

    你这个线程类本身就是在Client类内部调用的,不需要再实例化了
      

  3.   

    ThreadM类是Client类的内部类,它可以直接访问Client类的实例变量和实例方法.
      

  4.   

    面向对象,
    new Client().repaint(); // 这个改变的是你实例化后的对象,并不是当前对象.而 repaint(); // 在类的内部,默认是this.repaint(); 是指当前这个对象本身.
      

  5.   

    恩,楼上说的很对,直接调用repaint()是当前对象的repaint,加个new Client()每次都要重新初始化对象,所以那个圆就停在原地不动.