Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查找swing timer已运行的时间_Java_Swing_Timer - Fatal编程技术网

Java 查找swing timer已运行的时间

Java 查找swing timer已运行的时间,java,swing,timer,Java,Swing,Timer,我正在做一个秒表,我知道如何做每件事,除了找出我的计时器对象已经运行了多长时间。是否有类似于timer.getElapsedTime()的方法或类似的方法 编辑:有数字说00。每一秒它都需要增加。我的思维过程是seconds=timer.getElapsedTime(); secs.setText(秒)设置计时器时,只需将System.currentTimeMillis()保存在某个变量中,并在需要经过的时间时将该值与当前返回值进行比较 编辑:使计时器每秒触发一次。请注意,每次触发计时器时,我们

我正在做一个秒表,我知道如何做每件事,除了找出我的计时器对象已经运行了多长时间。是否有类似于
timer.getElapsedTime()
的方法或类似的方法

编辑:有数字说00。每一秒它都需要增加。我的思维过程是
seconds=timer.getElapsedTime();

secs.setText(秒)
设置计时器时,只需将System.currentTimeMillis()保存在某个变量中,并在需要经过的时间时将该值与当前返回值进行比较

编辑:使计时器每秒触发一次。请注意,每次触发计时器时,我们都会重新设置计时器,以防止延迟和处理时间累积

package timertest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TimerTest implements ActionListener {

    JLabel timeDisplay;
    long startTime;
    Timer timer;
    int seconds;

    public void createAndShowGUI()  {
        JFrame frame=new JFrame("Stopwatch");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        timeDisplay=new JLabel("0");
        frame.getContentPane().add(timeDisplay);
        frame.pack();
        frame.setVisible(true);

        startTime=System.currentTimeMillis();
        seconds=1;
        timer=new Timer(1000, this);
        timer.setRepeats(false);
        timer.start();
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TimerTest().createAndShowGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        long now=System.currentTimeMillis();
        long elapsed=now-startTime;
        seconds++;
        timeDisplay.setText(elapsed+" Milliseconds since start");
        timer.setInitialDelay((int)(startTime+seconds*1000-now));
        timer.start();
    }
}

您可以创建自己的计时器类。(或从当前计时器类继承并添加此功能)


我应该再指定一点。我需要数字每秒递增。@ChrisVachon什么数字?秒表显示00:00:00,下一秒应该显示00:00:01,然后是00:00:02等等。我可能应该指定。但如果我能知道计时器运行了多长时间,我知道该怎么做。在这种情况下,我会在开始时获取当前时间,并将计时器精确设置为1000毫秒。每次计时器过期时,获取当前时间,更新显示,然后将计时器重新设置为(原始时间+秒-当前时间)-这样,您可能会将计时器设置为950毫秒左右,以考虑延迟和处理时间,但您的总时间将很好地匹配,并且您根本不必轮询计时器。我将如何确切地做到这一点?类似于
int秒=0;定时器(1000,ActionListener);secs.setText(toString(second++));timer.restart()在循环中?您能为问题提供更广泛的上下文吗?我怀疑这根本不是你想做的,好像你在用定时器,你不应该需要它。你能提供一些你目前掌握的代码吗?
public class Timer
{
    private long startTime = 0;

    public void start()
    {
        startTime = System.currentTimeMillis();
    }

    public int getElapsedTime() 
    {
        return (System.currentTimeMillis() - startTime) / 1000 //returns in seconds
    }
}