Java GUI使用线程冻结
我是线程编程的新手,我启动了一个线程,在一个框架内移动一个JPanel。但是当我调用该方法时,框架完全冻结,我必须强制它关闭 这是我实现的简化方法Java GUI使用线程冻结,java,multithreading,frame,Java,Multithreading,Frame,我是线程编程的新手,我启动了一个线程,在一个框架内移动一个JPanel。但是当我调用该方法时,框架完全冻结,我必须强制它关闭 这是我实现的简化方法 public class Thread extends Thread{ public void movePanel(Frame f) { try { long t = f.getTime(); while (true) {
public class Thread extends Thread{
public void movePanel(Frame f) {
try {
long t = f.getTime();
while (true) {
f.changeColor();// changes Colour of current JPanel
Thread.sleep(t);
while (true) {
f.setNextPosition(direction);// sets the Position, where the JPanel moves next
p = f.getNextPosition();
f.moveForward();// moves in given direction
Thread.sleep(dauer);
if (p == null)// checks if the next Position is accessible {
f.moveDown();
Thread.sleep(t);
while (true) {
f.moveBackwards();
Thread.sleep(t);
if (p == null) {
f.moveDown();
Thread.sleep(t);
}
}
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
什么会导致问题?
非常感谢您的帮助。Thread.sleep()正在阻止当前线程至少达毫秒。重要的是“阻塞”!查找线程。等待(t);您有一个Thread.sleep(),并且有一段时间(true)从未完成执行,因此您一直处于循环中。对于动画,请使用