【Java】Java計時器(秒錶)

來源:互聯網
上載者:User

標籤:main   exce   action      ima   tde   AC   sys   err   

50506759

應用程式名稱:Java計時器

用到的知識:Java GUI編程

開發環境:win8+eclipse+jdk1.8

功能說明:計時功能,精確到1毫秒,可暫停。

:

原始碼:

import javax.swing.*;  import java.awt.HeadlessException;  import java.awt.BorderLayout;  import java.awt.FlowLayout;  import java.awt.Font;  import java.awt.event.ActionListener;  import java.awt.event.ActionEvent;     /**  * 計時器  */  public class Timer extends JFrame {         /**     *      */    private static final long serialVersionUID = 1L;     private static final String INITIAL_LABEL_TEXT = "00:00:00 000";         // 計數線程      private CountingThread thread = new CountingThread();         // 記錄程式開始時間      private long programStart = System.currentTimeMillis();         // 程式一開始就是暫停      private long pauseStart = programStart;         // 程式暫停總時間      private long pauseCount = 0;         private JLabel label = new JLabel(INITIAL_LABEL_TEXT);         private JButton startPauseButton = new JButton("開始");         private JButton resetButton = new JButton("清零");         private ActionListener startPauseButtonListener = new ActionListener() {          public void actionPerformed(ActionEvent e) {              if (thread.stopped) {                  pauseCount += (System.currentTimeMillis() - pauseStart);                  thread.stopped = false;                  startPauseButton.setText("暫停");              } else {                  pauseStart = System.currentTimeMillis();                  thread.stopped = true;                  startPauseButton.setText("繼續");              }          }      };         private ActionListener resetButtonListener = new ActionListener() {          public void actionPerformed(ActionEvent e) {              pauseStart = programStart;              pauseCount = 0;              thread.stopped = true;              label.setText(INITIAL_LABEL_TEXT);              startPauseButton.setText("開始");          }      };         public Timer(String title) throws HeadlessException {          super(title);          setDefaultCloseOperation(EXIT_ON_CLOSE);          setLocation(300, 300);          setResizable(false);             setupBorder();          setupLabel();          setupButtonsPanel();             startPauseButton.addActionListener(startPauseButtonListener);          resetButton.addActionListener(resetButtonListener);             thread.start(); // 計數線程一直就運行著      }         // 為表單面板添加邊框      private void setupBorder() {          JPanel contentPane = new JPanel(new BorderLayout());          contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));          this.setContentPane(contentPane);      }         // 配置按鈕      private void setupButtonsPanel() {          JPanel panel = new JPanel(new FlowLayout());          panel.add(startPauseButton);          panel.add(resetButton);          add(panel, BorderLayout.SOUTH);      }         // 配置標籤      private void setupLabel() {          label.setHorizontalAlignment(SwingConstants.CENTER);          label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));          this.add(label, BorderLayout.CENTER);      }         // 程式入口      public static void main(String[] args) {          try {              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());          } catch (Exception e) {              e.printStackTrace();          }             Timer frame = new Timer("計時器");          frame.pack();          frame.setVisible(true);      }         private class CountingThread extends Thread {             public boolean stopped = true;             private CountingThread() {              setDaemon(true);          }             @Override          public void run() {              while (true) {                  if (!stopped) {                      long elapsed = System.currentTimeMillis() - programStart - pauseCount;                      label.setText(format(elapsed));                  }                     try {                      sleep(1);  // 1毫秒更新一次顯示                } catch (InterruptedException e) {                      e.printStackTrace();                      System.exit(1);                  }              }          }             // 將毫秒數格式化          private String format(long elapsed) {              int hour, minute, second, milli;                 milli = (int) (elapsed % 1000);              elapsed = elapsed / 1000;                 second = (int) (elapsed % 60);              elapsed = elapsed / 60;                 minute = (int) (elapsed % 60);              elapsed = elapsed / 60;                 hour = (int) (elapsed % 60);                 return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);          }      }  }  

 

【Java】Java計時器(秒錶)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.