我只想用空格键启动/停止停车表。我已经做好了KeyListeners,只有当您按下/释放空格键时,它们才会被激活。
到目前为止我尝试过的是:
我尝试创建一个秒表类,它应该计算我第一次按下空间和第二次按下空间之间的时间差。我试过如下:
public class Stopwatch {
public Stopwatch(int i) {
long time = System.currentTimeMillis();
if(i%2==1){
System.out.println("Timer Started at: " + time);
}else{
System.out.println("Timer stopped at: " + System.currentTimeMillis());
System.out.println("Time diff: " + (time - System.currentTimeMillis()));
}
}
}每秒钟按空格键,int i就会增加。
我知道这里的问题是,每次我开始这个类时,time都会被重置为System.currentTimeMillis(),因为我只按空格键。因此,差别总是0。
我怎样才能改变这种情况,从而节省我第一次按下的空间的时间?
下面是带有密钥侦听器的类。忽略了填鸭器类,这与我的问题无关。
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class StoppuhrFrame extends JFrame {
JLabel time, scramble;
public StoppuhrFrame() {
time = new JLabel("00:00:00");
time.setBounds(162, 45, 325, 80);
time.setFont(new Font("Arial", 100, 80));
add(time);
scramble = new JLabel("Scramble: ");
scramble.setBounds(165, 15, 370, 16);
add(scramble);
//Scrambler scrambler = new Scrambler(scramble);
addKeyListener(new timer());
setTitle("Cube Timer");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(650, 270);
setVisible(true);
}
int i = 1;
public class timer implements KeyListener {
@Override
public void keyPressed(KeyEvent keyEvent) {
if(keyEvent.getKeyCode()==32){
new Stopwatch(i);
i++;
}
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == 32) {
if(i%2==0){
//new Scrambler(scramble);
}
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
}提前谢谢。
发布于 2016-11-14 12:42:16
对不起,我试图添加它作为评论,但不让我发布代码。下面是我提到的示例类:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Example implements KeyListener {
private static long hitTime = -1;
@Override
public void keyPressed(KeyEvent arg0) {
// This will allways return the time when first space bar was clicked
long start = getHitTime();
long actualTime = System.currentTimeMillis();
// In this case get the seconds..
long diff = (actualTime - start) / 1000;
System.out.println(String.format("TIme diff: %s", diff));
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public synchronized static long getHitTime() {
if (hitTime < 0) {
hitTime = System.currentTimeMillis();
}
return hitTime;
}
}您也不需要秒表类,因为在第一个键单击之后,时间将开始传递(在本例中,没有检测到特定的键,因此任何键点击都会触发事件),并计算第一次单击和millis中最后一次单击之间的时间差,我将其除以一千,以获得两次点击之间的秒数。
希望这能有所帮助
发布于 2016-11-14 11:48:08
将时间作为用负值初始化的StopWatch用户类的静态属性(据我所知,它可能是侦听器实例)。然后应用一个同步方法来获取time属性的值(静态同步以确保线程在更新该变量时对它的安全访问)。并应用单例模式is更新该值,考虑到当按下空格键时,值>0是可能的:
private static long hitTime = -1;
//...
public synchronized static long getFirstSpaceHitMillis() {
if (hitTime < 0) {
// this if ensures the variable will be only updated at first hit
hitTime = System.currentTimeMillis();
}
return hitTime;
}在StopWatch类中,init时间将通过对getFirstSpaceHitMillis方法的静态调用来检索,因此您将得到第一个空格被击中的时间。
希望能对此有所帮助,并对没有附加完整的实施表示歉意。
https://stackoverflow.com/questions/40587641
复制相似问题