问题:1、绘画面板主要是UI线程执行,所以要注意:如果你在主线程中休眠,UI线程也会休眠,导致界面覆盖、卡顿等现象
解决:开辟一个异步线程,定时休眠,在调用repaint()重绘面板
如:
new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myPanel.repaint();//myPanel自定义的绘画面板
}
}
}).start();
问题:2、调用repaint(),重绘面板时,界面卡死,覆盖等
解决:因为repaint()会调用paint()重绘,所以要在paint()中继承父类paint(g)即可解决
public void paint(Graphics g) {