从创建的JFrame JFrame的时出错 [英] Error when creating JFrame from JFrame
问题描述
我有它是在GameInitializer类发生在一些配置参数的构造函数启动一个应用程序,工作正常,并在JFrame。我试图创建一个图形用户界面中,用户可以指定这些配置参数,然后点击提交。当用户点击提交创建一个新的GameInitializer对象。我得到的错误是:
I have an application that is works fine and the JFrame for it is launched in the constructor of a GameInitializer class which takes in some config parameters. I have tried to create a GUI in which allows the user to specify these config parameters and then click submit. When the user clicks submit a new GameInitializer object is created. The error I am getting is:
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)
提交一次被调用时执行该code:
once submit is called this code is executed:
values assigned to parames...
new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
然后在GameInitializer构造code是:
Then code in the GameInitializer constructor is:
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();
}
它基本上落下下来,并最终调用这块code在HumanView类:
It basically cascades down and eventually calls this piece of code in the HumanView class:
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();
}
}
}
你有任何想法,为什么?
Do you have any idea why?
推荐答案
invokeAndWait的点()
是另一个线程等待一些事件做分派线程(EDT)。由于这code已经在美国东部时间执行,这是足以称之为直接,而不是创建一个Runnable。
The point of invokeAndWait()
is for another thread to wait for something to be done in the event dispatching thread (EDT). Since this code is already executing on the EDT, it's sufficient to call it directly rather than creating a Runnable.
所以这code不应被任何东西所包围:
So this code should not be surrounded by anything:
gamePanel.update(event, handState, player);
validate();
这篇关于从创建的JFrame JFrame的时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!