【发布时间】:2018-06-10 22:42:56
【问题描述】:
我正在用 Java 创建一个纸牌游戏,并想描绘发牌的外观。我创建了 JLabels 并通过使用 .setVisible(true) 使它们可见,并希望在每张可见的卡之间延迟。 我试过使用 wait() 和 Thread.sleep() 但都没有奏效。 wait() 给了我错误,Thread.sleep 只是暂停了整个程序,而不仅仅是每次出现。 (此代码在按下“下一步”按钮时运行,因此它在 actionPerformed 方法中)
注意:我是初学者..请保持简单:)
else if(eventName.equals("Next"))
{
rules.setVisible(false);
next.setVisible(false);
inst.setVisible(false);
cardDeck.setVisible(true);
discard.setVisible(true);
tot.setVisible(true);
specialCards.setVisible(true);
// delay here
userCard1.setVisible(true);
// delay here
compCard1.setVisible(true);
// delay here
userCard2.setVisible(true);
// delay here
compCard2.setVisible(true);
// delay here
userCard3.setVisible(true);
// delay here
compCard3.setVisible(true);
}
【问题讨论】:
-
你必须为每个视图设置动画
-
这最终会在事件线程上执行吗?例如在像 here 这样的 ActionListener 中
-
不是在动作监听器中,而是在actionPerformed方法中
-
那不作为
ActionListener接口中定义的actionPerformed使用??? -
好吧,你不应该在 Event Dispatch Thread 上使用 sleep (block) - 这会阻塞 GUI,也就是说,没有更新...试试
SwingWorker(在 StackOverflow 上搜索,这是一个很好的已知问题...但我现在搜索它为时已晚,抱歉)