问题如题://为什么用Panel小球就能动起来而JPanel就动不起来怎么回事?它们俩的区别是什么?import java.awt.*;import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
public class Demo {
@SuppressWarnings("deprecation")
public static void main(String args[]){
Frame frame=new Frame();
frame.setSize(400,500);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
e.getWindow().dispose();
System.exit(0);
}
});
Mypanel mp=new Mypanel();
frame.add(mp);
Thread t = new Thread(mp);
t.start();
frame.show();
}
}
class Mypanel extends Panel implements Runnable{//为什么用Panel小球就能动起来而JPanel就动不起来怎么回事?
int x=30;
int y=30;
public void paint(Graphics g){
g.fillOval(x, y, 10,10);
}
public void run() {
while(true){
x++;
y++;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}
|
|