1

我有一些代码显示两个按钮的 GUI,目标是按一个按钮两次以显示按钮单击之间花费的时间(以毫秒为单位)。

虽然我的问题是时间总是0。有什么建议吗?我还想实现一种方法来获取按钮 a 和按钮 b 的点击之间的时间。有小费吗?谢谢。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonViewer 
{
    static int countA = 0;
    static int countB = 0;
public static void main( String[] args ) 
{
JFrame frame = new JFrame();
GridLayout layout = new GridLayout(2,2);
frame.setLayout(layout);
JButton buttonA = new JButton("Button A");
frame.add(buttonA);

class ClickListenerOne implements ActionListener 
{
    public void actionPerformed( ActionEvent event ) 
    {
        countA++;
        long StartA = System.currentTimeMillis();

        if (countA % 2 == 0)
        {
             long EndA = System.currentTimeMillis();
             long differenceA = (EndA - StartA);
             System.out.println(differenceA + " Elapsed");  
        }

    }
}

JButton buttonB = new JButton("Button B");
frame.add(buttonB);

class ClickListenerTwo implements ActionListener 
{
    public void actionPerformed( ActionEvent event ) 
    {
        countB++;
        long StartB = System.currentTimeMillis();
        if (countB % 2 == 0)
        {
             long EndB = System.currentTimeMillis();
             long differenceB = (EndB - StartB);
             System.out.println(differenceB + " Elapsed");  
        }
    }
}

ActionListener mButtonAClicked = new ClickListenerOne();
buttonA.addActionListener(mButtonAClicked);

ActionListener mButtonBClicked = new ClickListenerTwo();
buttonB.addActionListener(mButtonBClicked);

frame.setSize( 200, 200 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
}
4

3 回答 3

1

在按钮的第一次点击中设置StartA,即

 long StartA;
public void actionPerformed( ActionEvent event )
{
    countA++;
    if (countA % 2 != 0)
  StartA = System.currentTimeMillis();

    if (countA % 2 == 0)
    {
         long EndA = System.currentTimeMillis();
         long differenceA = (EndA - StartA);
         System.out.println(differenceA + " Elapsed");
    }

这将为您提供第一次点击和第二次点击之间的区别

于 2013-10-14T17:21:37.917 回答
0

在您的代码中:

class ClickListenerOne implements ActionListener {
 // int startA; ?
public void actionPerformed( ActionEvent event ) 
{
    countA++;
    long StartA = System.currentTimeMillis();  // you are reading  time 
                              //after mouse click event

    if (countA % 2 == 0)
    {
         long EndA = System.currentTimeMillis(); // variable name should not be 
                              //declared with capital letter
         long differenceA = (EndA - StartA);
         System.out.println(differenceA + " Elapsed");// then you are computing 
                                 //elapsed time in the same click event 
         // startA = endA; // updated the time after each event?
    }

}}

这样,您将不会在后续鼠标单击事件之间经过时间。这个想法是startTime在你的类减速中声明为成员。按照您的操作计算经过的时间,然后startTime通过将其设置为 来更新endTime

于 2013-10-14T17:23:08.613 回答
0

使用 System.nanotime() 而不是 System.currentTimeMillis()

public void actionPerformed( ActionEvent event ) 
{
    countA++;
    if(countA % 2 != 0){
    StartA = System.nanoTime();
    }

    if (countA % 2 == 0)
    {
         EndA = System.nanoTime();
         differenceA = EndA- StartA;
         System.out.println(differenceA); 


    }

}
于 2017-03-07T11:16:27.433 回答