这段代码基本上会在点击时显示卡片的背面。 showFace
功能设置图标和文本,因此暗示卡的正面。浅灰色的背景是背面。如果点击了不匹配的卡片,我首先打算showFace
短暂(2秒)而不是恢复到卡的背面。"但是,单击不匹配的卡时,图标和文本会立即闪烁并恢复为灰色背景。
尝试将2000毫秒更改为更高但无效。任何帮助表示赞赏。
else if (currentMode == 1){
//matched cards
if(checkCards(currentCard, names)){
showFace();
currentMode = 0;
currentCard = "";
deafTo(this);
}
//else non match, still checking mode
else{
showFace();
var timer: Timer = null;
val action = new ActionListener(){
override def actionPerformed(event : ActionEvent){
icon = null;
text = "";
background = Color.DARK_GRAY;
timer.stop();
}
};
timer = new Timer (2000, action);
timer.setInitialDelay(0);
timer.start();
}
}
def showFace()={
text = names;
horizontalTextPosition = Alignment.Center;
verticalTextPosition = Alignment.Bottom;
background = Color.WHITE;
val icons = new ImageIcon(path);
val newIc = icons.getImage();
val newIcons = newIc.getScaledInstance(100, 75,
java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newIcons);
repaint();
}
答案 0 :(得分:1)
这是因为你在构造函数
中设置了2000ms的初始延迟timer = new Timer(2000, action)
然后你将它覆盖到0ms:
timer.setInitialDelay(0);
删除此行,你应该很好。
答案 1 :(得分:0)
Javadoc http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
此类不提供实时保证:它使用Object.wait(long)方法调度任务。
Java 5.0引入了java.util.concurrent包,其中一个并发实用程序是ScheduledThreadPoolExecutor,它是一个线程池,用于以给定的速率或延迟重复执行任务。它实际上是Timer / TimerTask组合的更通用替代品,因为它允许多个服务线程,接受各种时间单位,并且不需要子类化TimerTask(只需实现Runnable)。使用一个线程配置ScheduledThreadPoolExecutor使其等同于Timer。