-4

我真的很困,感谢我的大学。我需要 Java 中的代码来Stopwatch显示时间的00:00:00(mm:ss:msms)格式。我想使用 Key 事件来运行、暂停和重置计时器。就像我按 S 秒表开始,P 暂停,R 重置一样。问题是我还想在团队的数字上添加关键事件,比如如果我按 1,“团队 1”会闪烁,最好是哔哔声,依此类推2 3 4 5。我无法理解如何做到这一点。

我写这个是为了在第二个打印时间只是为了尝试......

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

public class StopWatch2 extends JLabel
            implements KeyListener, ActionListener {

   private long startTime;                           

   private boolean running;  
   private Timer timer;  
   public StopWatch2() {
             super("  Press S  ", JLabel.CENTER);
      addKeyListener(this);
   }

   public void actionPerformed(ActionEvent evt) {

       long time = (System.currentTimeMillis() - startTime) / 1000;
       setText(Long.toString(time));
   }

   public void keyPressed(KeyEvent e) {

          int keyCode=e.getKeyCode();
      if (keyCode==KeyEvent.VK_S) {
                     running = true;
         startTime = e.getWhen(); 
         setText("Running:  0 seconds");
         if (timer == null) {
            timer = new Timer(100,this);
            timer.start();
         }
         else
            timer.restart();
      }
      if(keyCode==KeyEvent.VK_P)
      {

         timer.stop();
         running = false;
         long endTime = e.getWhen();
         double seconds = (endTime - startTime) / 1000.0;
         setText("Time: " + seconds + " sec.");
      }
   }
   public void keyTyped(KeyEvent e)
   {}
   public void keyReleased(KeyEvent e)
   {}

} 




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

public class Test2 extends JApplet {

   public void init() {

      StopWatch2 watch = new StopWatch2();
      watch.setFont( new Font("SansSerif", Font.BOLD, 24) );
      watch.setBackground(Color.white);
      watch.setForeground( new Color(180,0,0) );
      watch.setOpaque(true);
      getContentPane().add(watch, BorderLayout.CENTER);

   }

}

我在自己的 nm 上尝试一些东西,几乎是自学的,所以我无法理解出了什么问题

4

1 回答 1

0

你的意思是这样的:

/**
 * Stopwatch is a simple timer.
 */
public class Stopwatch {

    /**
     * Stopwatch() Initialises a stopwatch.
     */
    public Stopwatch() {
        // Your code here.
    }

    /**
     * elapsed() The elapsed time in milliseconds shown on the stopwatch.
     *
     * @return double  The elapsed time in milliseconds as a double.  Returns -1.0 if no meaningful
     * value is available, i.e. if the watch is reset or has been started and not stopped.
     */
    public double elapsed() {
        // Your code here.
    }

    /**
     * start() Starts the stopwatch and clears the previous elapsed time.
     */
    public void start() {
        // Your code here.
    }

    /**
     * stop() If the stopwatch has been started this stops the stopwatch and calculates the
     * elapsed time.  Otherwise it does nothing.
     */
    public void stop() {
        // Your code here.
    }

    /**
     * reset() Resets the stopwatch and clears the elapsed time.
     */
    public void reset() {
        // Your code here.
    }

    @Override
    public String toString() {
        // Your code here.
    }

} // end class Stopwatch
于 2012-08-02T19:39:47.913 回答