从JFrame创建JFrame时出错
我有一个运行良好的应用程序,它的JFrame是在GameInitializer类的构造函数中启动的,该类带有一些配置参数。我尝试创建一个GUI,在其中允许用户指定这些配置参数,然后单击“提交”。当用户单击提交时,将创建一个新的GameInitializer对象。我得到的错误是:
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.view.HumanView.update(HumanView.java:43)
一旦提交被调用,将执行以下代码:
values assigned to parames... new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
然后GameInitializer构造函数中的代码是:
public GameInitializer(String playerName, AbstractPlayer opponent, String blindStructureConfig, AbstractHandState handState){ beginGame(playerName, opponent, blindStructureConfig, handState);
}
public static void beginGame(String playerName, AbstractPlayer opponent, String blindStructureConfig, AbstractHandState handState){
AbstractDealer dealer;
BlindStructure.initialize(blindStructureConfig);
AbstractPlayer humanPlayer = new HumanPlayer(playerName, handState);
AbstractPlayer[] players = new AbstractPlayer[2];
players[0] = humanPlayer;
players[1] = opponent;
handState.setTableLayout(players);
for(AbstractPlayer player : players){
player.initialize();
}
dealer = new Dealer(players, handState);
dealer.beginGame();
它基本上是级联的,并最终在HumanView类中调用以下代码:
public void update(final Event event, final ReadableHandState handState, final AbstractPlayer player) { try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
gamePanel.update(event, handState, player);
validate();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
if(event.equals(Event.HAND_ENDING)){
try {
if(handState.wonByShowdown() || handState.isSplitPot()){
Thread.sleep(3500);
}
else{
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
你知道为什么吗?
回答:
关键invokeAndWait()
是让另一个线程等待事件分发线程(EDT)中的某些操作。由于此代码已在EDT上执行,因此直接调用它而不是创建Runnable就足够了。
因此,此代码不应包含任何内容:
gamePanel.update(event, handState, player); validate();
以上是 从JFrame创建JFrame时出错 的全部内容, 来源链接: utcz.com/qa/414812.html