简体   繁体   English

Swing GUI组件的计时器问题

[英]Timer problems with swing GUI components

This snippet of code essentially reveals the back side of the cards when clicked. 单击此代码段实质上揭示了卡的背面。 The showFace function sets the icon and text and therefore implying the front of the card. showFace函数可设置图标和文本,并因此暗示卡片的正面。 Light gray background is the back. 背面是浅灰色背景。 If a non matching card is clicked, I first intend to showFace for a brief moment ( 2 seconds) than revert to the "back side of the card." 如果单击了不匹配的卡片,我首先打算在短暂的一刻(2秒)内显示“ showFace ,而不是恢复到“卡片的背面”。 However, upon clicking a non matching card, the icon and text flashes instantaneously and reverts to its gray background. 但是,单击不匹配的卡后,图标和文本会立即闪烁并恢复为灰色背景。

Tried changing the 2000 milliseconds to something higher but no avail. 尝试将2000毫秒更改为更高的值,但无济于事。 Any help is appreciated. 任何帮助表示赞赏。

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();
}

This is because you set an initial delay of 2000ms in the constructor 这是因为您在构造函数中设置了2000ms的初始延迟

timer = new Timer(2000, action)

But then you overwrite it to 0ms by: 但是,您可以通过以下方式将其覆盖为0ms:

timer.setInitialDelay(0);

Remove this line and you should be good. 删除此行,您应该会很好。

You can check here Swing Timer API. 您可以在此处检查Swing Timer API。
And see some examples here . 在此处查看一些示例。

Javadoc http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html Javadoc http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html

This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method. 此类不提供实时保证:它使用Object.wait(long)方法调度任务。

Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. Java 5.0引入了java.util.concurrent包,其中的并发实用程序之一是ScheduledThreadPoolExecutor,它是一个线程池,用于以给定的速率或延迟重复执行任务。 It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). 实际上,它是Timer / TimerTask组合的更通用的替代品,因为它允许多个服务线程,接受各种时间单位,并且不需要子类化TimerTask(只需实现Runnable)。 Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer. 使用一个线程配置ScheduledThreadPoolExecutor使其等效于Timer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM