如何仅使用空格键启动/停止秒表?(How can I start/stop a stop watch with only the spacebar?)

编程入门 行业动态 更新时间:2024-10-23 12:32:22
如何仅使用空格键启动/停止秒表?(How can I start/stop a stop watch with only the spacebar?)

我想仅使用空格键开始/停止秒表。 我已经创建了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。

我怎样才能改变这一点,以便我能以某种方式节省我第一次按空间的时间?

这是Keylisteners的课程。 忽略Scrambler类,它与我的问题无关。

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 } }}

提前致谢。

I want to start/stop a stop watch only using the spacebar. I already made the KeyListeners, and that they only get activated when you press/release the spacebar.

What I tried so far:

I tried creating a stopwatch class, which SHOULD calculate the time difference between me pressing space for the first time, and second time. I tried it as follows:

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 increases with every second spacebar press.

I know the problem here is that everytime I start this class, time is getting reset to System.currentTimeMillis() because I only press Spacebar. Thus the difference is always 0.

How can I change this so that I can somehow save the time I first pressed space?

Here is the class with the Keylisteners. Ignore the Scrambler class, it has nothing to do with my Problem.

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 } }}

Thanks in advance.

最满意答案

抱歉,我试图将其添加为评论,但没有让我发布代码。 HEre是我提到的示例类

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,我将它除以千分之一,以达到命中之间的秒数。

希望这可以帮助

Sorry i tried to add it as a comment but didn't let me to post the code. HEre is the example class i mentioned

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

You also dont need a stopwatch class as time will start passing after first key click (in this case not specific key was detected so any key hit will trigger the event), and will calculate the difference of time between first click and the last one in millis, I divide it by a thousand to get seconds between hits.

Hope this helps

更多推荐

本文发布于:2023-07-23 12:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231650.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:秒表   空格键   start   spacebar   watch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!