-4

私の大学のおかげで、私は本当に立ち往生しています。Stopwatch時間を00:00:00(mm:ss:msms)フォーマットで表示するJavaのコードが必要です。キーイベントを使用して、タイマーを実行、一時停止、リセットしたいと思います。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 に答える