Java实现计时器
?
public class MyTimer extends JFrame implements ActionListener {private static final String COUNT_TEXT = "00:00:00:000";// 計(jì)時(shí)線程private final CountingThread thread = new CountingThread();private final JLabel label = new JLabel(COUNT_TEXT);private final JButton jbt1 = new JButton("開(kāi)始計(jì)時(shí)");private final JButton jbt2 = new JButton("暫停計(jì)時(shí)");private final JButton jbt3 = new JButton("計(jì)時(shí)清零");//程序開(kāi)始時(shí)系統(tǒng)時(shí)間private final long programStart = System.currentTimeMillis();//暫停時(shí)系統(tǒng)時(shí)間private long pauseStart = programStart;//程序暫停的總時(shí)間private long pauseTime = 0;public MyTimer(String title) throws HeadlessException {super(title);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setResizable(false);initBorder();initLabel();initButtons();this.pack();this.setVisible(true);jbt1.addActionListener(this);jbt2.addActionListener(this);jbt3.addActionListener(this);thread.start();}//程序入口public static void main(String[] args) {new MyTimer("計(jì)時(shí)器");}//初始化計(jì)時(shí)框private void initBorder() {JPanel panel1 = new JPanel(new BorderLayout());panel1.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));this.setContentPane(panel1);}// 初始化計(jì)時(shí)板private void initLabel() {label.setHorizontalAlignment(SwingConstants.CENTER);//設(shè)置背景黑底白字label.setOpaque(true);label.setBackground(Color.BLACK);label.setForeground(Color.WHITE);label.setFont(new Font(label.getFont().getName(), BOLD, 50));this.add(label, BorderLayout.CENTER);}// 初始化按鈕private void initButtons() {JPanel panel = new JPanel(new FlowLayout());panel.add(jbt1);panel.add(jbt2);jbt2.setEnabled(false);panel.add(jbt3);add(panel, BorderLayout.SOUTH);}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == jbt1) {//暫停時(shí)間=累計(jì)暫停時(shí)間+現(xiàn)在系統(tǒng)時(shí)間-程序最近一次暫停系統(tǒng)時(shí)間pauseTime += (System.currentTimeMillis() - pauseStart);thread.stopped = false;jbt1.setEnabled(false);jbt2.setEnabled(true);} else if (e.getSource() == jbt2) {jbt1.setEnabled(true);//更新最近一次暫停時(shí)間pauseStart = System.currentTimeMillis();thread.stopped = true;jbt2.setEnabled(false);} else if (e.getSource() == jbt3) {pauseStart = programStart;pauseTime = 0;thread.stopped = true;label.setText(COUNT_TEXT);jbt1.setEnabled(true);jbt2.setEnabled(false);}}private class CountingThread extends Thread {public boolean stopped = true;@Overridepublic void run() {while (true) {if (!stopped) {//當(dāng)前計(jì)時(shí)=當(dāng)前系統(tǒng)時(shí)間-程序開(kāi)始時(shí)間-總的暫停時(shí)間long timeKeeping = System.currentTimeMillis() - programStart - pauseTime;label.setText(format(timeKeeping));}try {// 1毫秒更新一次顯示sleep(1);} catch (InterruptedException e) {e.printStackTrace();System.exit(1);}}}//格式化計(jì)時(shí)器private String format(long timekeeping) {int hour, minute, second, milli;milli = (int) (timekeeping % 1000);timekeeping = timekeeping / 1000;second = (int) (timekeeping % 60);timekeeping = timekeeping / 60;minute = (int) (timekeeping % 60);timekeeping = timekeeping / 60;hour = (int) (timekeeping % 24);return String.format("%02d:%02d:%02d:%03d", hour, minute, second, milli);}} }總結(jié)
- 上一篇: isp 图像算法(三)之anti-ali
- 下一篇: Python + Paramiko实现s