黑马程序员技术交流社区
标题:
关于JPanel与panel的区别!
[打印本页]
作者:
yang649981273
时间:
2014-7-5 21:26
标题:
关于JPanel与panel的区别!
问题如题://为什么用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();
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2