2012-08-02 24 views
-4

我真的被卡住了,感謝我的大學。 我需要在Java代碼有一個Stopwatch其中顯示時間00:00:00(mm:ss:msms)格式。我想使用Key事件來運行並暫停並重置計時器。就像我按S鍵秒錶開始,P暫停和R復位。 事情是我還想爲球隊添加關鍵數字的關鍵事件,比如如果我按下1,「球隊1」閃動,最好是用嘟嘟聲等等2345。即時通訊不能夠了解如何做到這一點。如何使用java製作測驗系統的秒錶

我寫了這個打印時間僅次於剛剛嘗試...

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); 

    } 

} 

IM在我自己的納米相當多的自我嘗試的東西教這樣的IM無法理解怎麼回事錯

+3

那麼你的問題是什麼?你期待有人只爲你寫這個程序嗎? – 2012-08-02 19:05:01

+0

到目前爲止你有什麼? – Bobulous 2012-08-02 19:06:53

+1

*「...感謝我的大學」*。 *您採取了哪些措施來解決問題? – 2012-08-02 19:08:21

回答

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 
+0

有點,我想要的是秒錶應該集中對齊,它應該顯示時間變化, – user1572212 2012-08-02 19:43:18

+1

在我看來,你正在嘗試在一個地方做很多事情。將秒錶代碼分成自己的類。根據需要從用戶界面方法調用該類。用它們之間的最小交互分離出你的對象。這使得設計和編碼更容易。在你開始編碼之前,你確實在紙上設計了這個程序,不是嗎? – rossum 2012-08-02 20:30:01

+0

@ rossum-你是說在我將自己編碼到一個角落之前,應該有某種思維過程,我必須自己編碼?!?聽起來很好,建議!+1 – ChiefTwoPencils 2012-08-03 06:16:46