我有这段代码
// If we click on an unfolded card
if (card.clicked == false) {
clicksCount++;
clickedCards[clicksCount - 1] = card;
card.showCardImage();
if (clicksCount == 2) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
// Loop through clicked cards and if they don't have the
// same ID fold them
if (clickedCards[0].cardID != clickedCards[1].cardID) {
for (int k = 0; k < clickedCards.length; k++) {
clickedCards[k].setDefaultIcon();
clickedCards[k].clicked = false;
}
failedAttempts.setText("Failed Attempts: "
+ ++failedCount);
}
attemptsCount++;
attempts.setText("Attempts " + attemptsCount);
clicksCount = 0; // reset click counter
}
}
它适用于我的 pexeso 游戏。第一张卡可以很好地显示卡的图像,但是当我第二次单击时,它不会显示它的图像。它只会等待,然后折叠第一张牌。
我知道这是因为线程 hibernate ,但不知道该怎么办。
如果有任何想法或建议,我会很高兴。
最佳答案
我猜这是一个 Swing GUI(你从来没有告诉我们)。如果是这样,那么您是对的:调用 Thread.sleep(...)
在 Swing 事件线程上将使整个 GUI 进入休眠状态,包括所有 GUI 绘制。而是使用延迟设置为 1000 毫秒的 Swing 计时器。在您的计时器的 ActionListener 中,您将把卡改回原来的状态。您将通过 setter 方法告诉计时器不要重复,setRepeats(false)
.
关于java - 代码不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22721098/