如何使用java为测验系统制作秒表

如何使用java为测验系统制作秒表,java,applet,keyevent,Java,Applet,Keyevent,多亏了我的大学,我真的被卡住了。 我需要Java代码来创建一个Stopwatch,它以00:00:00(mm:ss:msms)格式显示时间。我想使用按键事件来运行、暂停和重置计时器。比如,如果我按下S,秒表启动,P暂停,R复位。 问题是,我还想为团队添加数字上的关键事件,比如如果我按1,“团队1”闪烁,最好是发出嘟嘟声,然后按2345。我不知道该怎么做 我写这篇文章是为了以秒打印时间,只是为了尝试 import java.awt.event.*; import javax.swing.*; p

多亏了我的大学,我真的被卡住了。 我需要Java代码来创建一个
Stopwatch
,它以
00:00:00(mm:ss:msms)
格式显示时间。我想使用按键事件来运行、暂停和重置计时器。比如,如果我按下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);

   }

}

我自己在试东西,我几乎是自学成才,所以我不知道出了什么问题

你是说:

/**
 * 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

那么你的问题是什么?你希望有人为你写这个程序吗?“…感谢我的大学”。你采取了什么措施来解决这个问题?@giovanni:那最好了@user1515834我编辑了我的帖子并添加了我想要的内容tried@DougRamsey刚刚编辑了我的帖子,我想要的是秒表应该居中对齐,它应该显示时间的变化,在我看来,你试图在一个地方做很多事情。将秒表代码分离到自己的类中。根据需要从用户界面方法调用该类。以最小的相互作用分离对象。这使得设计和编码更容易。在你开始编码之前,你确实在纸上设计了这个程序,不是吗?@rossum-你是说在我把自己编码到一个角落之前,应该有某种思维过程参与其中?!?听起来不错的建议+1.