使用键盘适配器结束计时器? [英] Ending a timer using keyadapters?

查看:37
本文介绍了使用键盘适配器结束计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在创建这个反应时间游戏,显示一个蓝色球,一旦你看到那个蓝色球,你就按下向上箭头键,一旦它变成蓝色,你就会得到你的反应时间.但是,我在创建计时器时遇到了麻烦.我希望计时器在球实际上变成蓝色时开始,并希望在用户按下向上箭头时结束计时器.'

So, I am creating this reaction time game that shows a blue ball and once you see that blue ball, you press up arrow key to get your reaction time once it turned blue. However, I am having trouble in creating a timer. I want the timer to start once the ball actually turns blue and want that timer to end once the user presses up arrow.'

这是我希望球变成蓝色之前游戏的样子

这就是我希望它变成蓝色后的样子,反应时间

这是我的代码.截至目前,一切正常,只是我需要帮助的计时器部分.我想在球变成蓝色时立即创建一个计时器,并希望在用户按下向上箭头后该计时器结束,但我不知道如何...

here is my code. As of right now, everything works fine its just the timer part I need help with. I want to create a timer as soon as the ball turns blue and want that timer to end once the user presses up arrow but I just dont know how...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;


public class Game extends JPanel implements KeyListener
{

private JLabel start, main, time;
private ImageIcon constant, react;

final int width = 600;
final int height = 600;

private Timer replace;


private Random random;
private int randTime;

private long startTime;
private long stopTime;
private long reactionTime;

public Game()
{
  addKeyListener(this);
  setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

setPreferredSize(new Dimension(width, height));
setBackground(Color.black);

start = new JLabel("Click Up Arrow when you see a blue ball");
start.setForeground(Color.white);
start.setAlignmentX(Component.CENTER_ALIGNMENT);
add(start);
 
constant = new ImageIcon("constantCircle.png");
main = new JLabel(constant);
main.setAlignmentX(Component.CENTER_ALIGNMENT);


randomTime();
replace = new Timer(randTime, timeListener);
replace.setRepeats(false);
replace.start();

timeTracker();
reactionTime = stopTime - startTime;
time = new JLabel("" + reactionTime);

add(time);
add(main);

}


public void randomTime()
{
  random = new Random();
  int max = 8000;
  randTime = random.nextInt(max);

}


ActionListener timeListener = new ActionListener()
{
  public void actionPerformed (ActionEvent e)
  {
    react = new ImageIcon("reactCircle.png");
    main.setIcon(react);
  
  }
};

public void timeTracker()
{
 if (replace.isRunning())
 {
   startTime = System.currentTimeMillis();
 }

}

public void keyPressed (KeyEvent e)
{  
  int key = e.getKeyCode();

  if (key == KeyEvent.VK_SPACE)
  {
    stopTime = System.currentTimeMillis();      
  }

}

public void keyReleased(KeyEvent e)
{
}

public void keyTyped (KeyEvent e)
{
}

}

推荐答案

不需要计时器.定时器用于以指定的时间间隔生成事件.你不知道用户什么时候会做出反应,所以你不能使用计时器.

There is no need for a Timer. A Timer is used to generate an event at a specified time interval. You don't know when the user will react so you can't use a Timer.

就您而言,您需要做的就是:

In your case all you need to do is:

  1. 设置您的开始时间"设置蓝色球图标时的当前时间变量.

  1. set your "startTime" variable to the current time when the blue ball icon is set.

那么你需要设置stopTime"向上"时的变量键被按下.此时,您将计算您的反应时间".

then you need to set the "stopTime" variable when the "Up" key is pressed. At this time you would then calculate your "reactionTime".

问题在于您的 KeyListener 不起作用,因为 KeyEvent 仅被分派给具有焦点的组件,并且默认情况下 JPanel 没有焦点.

The issue is that your KeyListener doesn't work, because a KeyEvent is only dispatched to the component with focus and by default a JPanel doesn't have focus.

解决方案是使用Key Bindings,而不是KeyListener.即使面板没有焦点,也可以将键绑定配置为接收 KeyEvent.

The solution is to use Key Bindings, not a KeyListener. A Key Binding can be configured to receive the KeyEvent even when the panel doesn't have focus.

阅读 Swing 教程中关于如何使用键绑定的部分 了解更多信息.这个站点上也有很多使用键绑定的例子.

Read the section from the Swing tutorial on How to Use Key Bindings for more information. There are also plenty of example on this site that use key bindings.

这篇关于使用键盘适配器结束计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆