Aly*_*Aly 6 java events swing awt
我之前的问题出现问题.我还在代码库中的SwingUtillities.invokeAndWait
其他地方有代码,但是当我删除它时,gui不会刷新.如果我不删除它我得到的错误是:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(Unknown Source)
at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)
at game.player.humanplayer.model.HumanPlayer.act(HumanPlayer.java:69)
Run Code Online (Sandbox Code Playgroud)
HumanPlayer.act中的代码是:
public Action act(final Action[] availiableActions) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
gui.update(availiableActions);
}
});
}
catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
synchronized(performedAction){
while(!hasPerformedAction()){
try {
performedAction.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
setPerformedAction(false);
}
return getActionPerfomed();
}
Run Code Online (Sandbox Code Playgroud)
调试时屏幕上的线程图像不显示: alt text http://img684.imageshack.us/img684/6669/69288941.png
堆栈的文本版本:
ui.startup.LoginScreen at localhost:51050
-> Deamon Thread [AWT-Windows] (Running)
-> Thread [AWT-Shutdown] (Running)
-> Thread [AWT-EventQueue-0] (Running)
-> Thread [DestroyJavaVM] (Running)
Run Code Online (Sandbox Code Playgroud)
答案不是拨打电话
new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
Run Code Online (Sandbox Code Playgroud)
从EDT开始,使它在新的(非EDT)线程上执行,以便稍后invokeAndWait
调用时它的运行正常,因为运行该命令的线程不是EDT.修订后的代码如下:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
}
});
t.start();
Run Code Online (Sandbox Code Playgroud)
invokeAndWait()
旨在从非 GUI 线程调用。它将一个Runnable
对象发送到将在那里执行的 GUI 线程。
将Runnable
对象从 GUI 线程发送到自身是没有意义的。它与调用同样的效果run()
上Runnable
直接对象。