package org.it315;
import java.awt.*;
import java.awt.event.*;import com.sun.xml.internal.ws.api.server.Adapter;public class TankClient extends Frame { int x=50,y=50; public void paint(Graphics g) {
Color c=g.getColor();
System.out.println(c);
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);

y+=5;
}
public void launchFrame()
{
this.setLocation(100,100);
this.setSize(600,600);
this.setResizable(false);
this.setTitle("TankWar");
this.setBackground(Color.GREEN);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setVisible(true);

}

private class PaintThread implements Runnable{
public void run(){
while(true){
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) { 
TankClient tc=new TankClient();
tc.launchFrame();
new Thread( new PaintThread()).start();
}}红线中的为什么会有错,请解释下

解决方案 »

  1.   

    new Thread( new PaintThread()).start();
    应该改成
    new Thread( tc.new PaintThread()).start();
    试试看
      

  2.   

    不能直接new一个内部类,视图直接new一个内部类是初学者经常犯的错误。
    如果坚持使用内部类,那么主函数改成public static void main(String[] args) { 
            TankClient tc=new TankClient();
            tc.launchFrame();
            PaintThread pt=tc.new PaintThread();
            new Thread(pt).start();
        }
      

  3.   

    main方法和class PaintThread同一级别的
    new Thread( new PaintThread()).start();
    这句话等价于new Thread( this.new PaintThread()).start();
    而this非静态的
    你可以换成静态类或者用new Thread( tc.new PaintThread()).start();