【问题标题】:JAVA time it takes between button clicks按钮点击之间的 JAVA 时间
【发布时间】:2017-03-07 19:16:27
【问题描述】:

我有一些代码显示两个按钮的 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);
}
}

【问题讨论】:

    标签: java button time


    【解决方案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");
        }
    

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

    【讨论】:

      【解决方案2】:

      在您的代码中:

      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 来更新它。

      【讨论】:

        【解决方案3】:

        使用 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); 
        
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-26
          • 1970-01-01
          • 1970-01-01
          • 2023-03-27
          • 2015-04-13
          • 1970-01-01
          • 1970-01-01
          • 2011-07-09
          相关资源
          最近更新 更多