【Java】Java计时器(秒表)

应用名称: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);  
        }  
    }  
}  


#include #define ulong unsigned long char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x90}; char tab_dp[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x58,0x00,0x10}; #define LED P0 //P0 为数码管的段选 #define WS P1 //P1 为数码管的位选 sbit time_shift=P3^0; //P3^0 为"移位"按键 sbit time_set=P3^1; //P3^1 为"修改"按键 sbit set_inc=P3^2; //P3^2 为"增加"按键 sbit time_run=P3^3; //P3^3 为"计时开始暂停"按键 sbit LIGHT=P2^0; sbit SPEAKER=P2^7; unsigned char sec,min,sec_ge,sec_shi,min_ge,min_shi,t; //定义秒,分,时,秒的个位,十位,分的个位,十位 int state; //秒表状态指示 0-计时暂停 1-正在倒计时 2-计时时间参数修改 int flag; //秒表修改数据指示 0-分钟十位 1-分钟个位 2-秒钟十位 3-秒钟个位 int blink; //-------------------------------------------------------------------- void init(); void display_run(); void display_pause(); void delay(int); void time_modify(); //-------------------------------------------------------------------- void main() { init(); state=0; flag=0; blink=1; t=0; LIGHT=1; SPEAKER=1; min=5; sec=0; sec_ge=0; //秒的个位和十位 sec_shi=0; min_ge=5; //分的个位和十位 min_shi=0; while(1) { if(time_set==0) //如果"调时"按键按下 { delay(5); //延时去抖 if(time_set==0) state=2; } if(state==0) { TR0=0; TR1=0; display_pause(); } if(state==1) { TR0=1; TR1=0; display_run(); } if(state==2) { TR1=1; time_modify(); } } } //-------------------------------------------------------------------- void init() { TMOD=0x11; EA=1; ET0=0; ET1=0; TH0=0x4d; TL0=oxfc; //开总中断 //定义定时器 0 工定时器 1 作与方式 1 //定时器 0 赋初值 TH1=15536/256; TL1=15536%256; //中断初始化 EX0=1; PX0=0; IT0=1; EX1=1; PX1=1; //定时器 1 赋初值 IT1=1; } //外中断 1 void INT_1() interrupt 2 using 0 { if( time_run==0)//表示按钮有按下 { delay(20); //延时 if(time_run==0) { state=(state+1)%2; LIGHT=1; SPEAKER=1; } } } //外中断 0 void INT_0() interrupt 0 using 1 { if( set_inc==0)//表示按钮有按下 { delay(20); //延时去抖 if(set_inc==0) { switch(flag) //分支结构,根据 flag 的值来判断哪一位加 1 { case 0: { min=(min+10)%60; break; } case 1: { min=(min+1)+min/10*10; break; } case 2: { sec=(sec+10)%60; break; } case 3: { sec=(sec+1)+sec/10*10; break; } default:break; } } } } //--------------------------------延时子函数----------------------------------- void delay(int x) { int i,j; for(i=x; i>0l; i--) for(j=255; j>0; j--) ; } //-------------------------------显示子函数-------------------------------------- void display_run() { if(TF0==1) //如果定时器溢出 { TF0=0; //清中断标志位 t++; if(t==20) { t=0; if(min==0&&sec==0) { min=60; } if(sec==0) { sec=59; min--; } else sec--; if(min==0&&sec>56) { SPEAKER=0; delay(20); SPEAKER=1; } if(min==0&&sec==0) { LIGHT=0; SPEAKER=0; delay(20); SPEAKER=1; delay(240); SPEAKER=0; delay(20); SPEAKER=1; delay(240); SPEAKER=0; delay(20); SPEAKER=1; delay(240); SPEAKER=0; delay(20); SPEAKER=1; delay(240); SPEAKER=0; delay(20); SPEAKER=1; state=4; min=5;//回归初始化 sec=0; sec_ge=0; //秒的个位和十位 sec_shi=0; min_ge=5; //分的个位和十位 min_shi=0; } } } sec_ge=sec; //秒的个位和十位 sec_shi=sec/10; min_ge=min; //分的个位和十位 min_shi=min/10; WS=0xfe; //循环扫描 LED=tab[sec_ge]; delay(1); WS=0xfd; LED=tab[sec_shi]; delay(1); WS=0xfb; LED=tab_dp[min_ge]; delay(1); WS=0xf7; LED=tab[min_shi]; delay(1); } void display_pause() { WS=0xfe; //循环扫描 LED=tab[sec_ge]; delay(1); WS=0xfd; LED=tab[sec_shi]; delay(1); WS=0xfb; LED=tab_dp[min_ge]; delay(1); WS=0xf7; LED=tab[min_shi]; delay(1); } void time_modify() { //修改位的闪烁状态 if(TF1==1) { TF1=0; blink=(blink+1)%2; } //修改具体哪个数码管指示 if(time_shift==0) { delay(60); if(time_shift==0) flag=(flag+1)%4; } // 修改状态下的数码管显示 switch(flag) //分支结构,根据 flag 的值来决定扫描状态 { case 0: { sec_ge=sec; //秒的个位和十位 sec_shi=sec/10; min_ge=min; //分的个位和十位 min_shi=min/10; WS=0xfe; //循环扫描 LED=tab[sec_ge]; delay(1); WS=0xfd; LED=tab[sec_shi]; delay(1); WS=0xfb; LED=tab_dp[min_ge]; delay(1); if(blink==1) { WS=0xf7; LED=tab[min_shi]; delay(1); } break; } case 1: { sec_ge=sec; //秒的个位和十位 sec_shi=sec/10; min_ge=min; //分的个位和十位 min_shi=min/10; WS=0xfe; //循环扫描 LED=tab[sec_ge]; delay(1); WS=0xfd; LED=tab[sec_shi]; delay(1); if(blink==1) { WS=0xfb; LED=tab_dp[min_ge]; delay(1); } WS=0xf7; LED=tab[min_shi]; delay(1); break; } case 2: { sec_ge=sec; //秒的个位和十位 sec_shi=sec/10; min_ge=min; //分的个位和十位 min_shi=min/10; WS=0xfe; //循环扫描 LED=tab[sec_ge]; delay(1); if(blink==1) { WS=0xfd; LED=tab[sec_shi]; delay(1); } WS=0xfb; LED=tab_dp[min_ge]; delay(1); WS=0xf7; LED=tab[min_shi]; delay(1); break; } case 3: { sec_ge=sec; //秒的个位和十位 sec_shi=sec/10; min_ge=min; //分的个位和十位 min_shi=min/10; if(blink==1) { WS=0xfe; //循环扫描 LED=tab[sec_ge]; delay(1); } WS=0xfd; LED=tab[sec_shi]; delay(1); WS=0xfb; LED=tab_dp[min_ge]; delay(1); WS=0xf7; LED=tab[min_shi]; delay(1); break; } default:break; } }
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值