package A;
import java.awt.Graphics;
import javax.swing.*;
public class xiancheng extends JFrame{
myPn my=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
xiancheng xin=new xiancheng();
}
public xiancheng()
{
my=new myPn();
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(my);
this.setLocationRelativeTo(null);
Thread t=new Thread(my);
while(true)
{
try {
Thread.sleep(10);
t.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
上面是代码 本人新人 就想画个会动的小球 报错了 错误如下
Exception in thread "main" 11//11
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at A.xiancheng.(xiancheng.java:29)
at A.xiancheng.main(xiancheng.java:12)
}
}
}
class myPn extends JPanel implements Runnable
{
int x=10;
int y=10;
public void paint (Graphics g)
{
super.paint(g);
g.fillOval(x, y, 10, 10);
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true)
{
try {
Thread.sleep(10);
this.repaint();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x++;
y++;
System.out.println(x+"//"+y);
}
}
}