计时器

package MYDemo;

/*定义一个计时器,点击开始按钮以后开始运行,每隔1ms开始计时,点击停止按钮停止计时,
 * 计时器常用timer类或多线程来实现,有空要了解一下Timer类。
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
 
// 定义一类,用来表示时间的运行
class TimeRun extends Thread implements  ActionListener    {
     	 static JFrame  jf=new  JFrame();//定义窗体
	JLabel jl=new JLabel("时间");
    JTextField jtf=new JTextField("                                            ");        
    JLabel jl2=new JLabel("秒");
    JButton start=new JButton("开始");
    JButton stop=new JButton("暂停");
    JButton clear=new JButton("清零");
        
    JPanel jp1=new JPanel();               //定义面板
    JPanel jp2=new JPanel();
    private float time=0;
     
  
	private static boolean Run=false;//运行标识
   public  static  void  main(String []args) {
 //定义窗体的参数
	   jf.setSize(300,100); 
       jf.setTitle("秒表");
        jf.setVisible(true);
        jf.setLocation(500,500);
       
	   TimeRun  tr=new  TimeRun();
	  tr.run();
	  }
    //定义小时,分钟,秒的显示
     private String time2str(float t) {//文本框中的内容显示
        int h = (int)t/36000;
        int m = ((int)t-h*36000)/600;
        double s = (t%600)/10.00;
        return String.format(" %02d : %04.1f", m,s);
    }      
//向窗体中加入各种组件并设置其位置。
    public TimeRun(){
    	
        jl.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        jl2.setAlignmentX(JLabel.CENTER_ALIGNMENT);       
        jtf.setHorizontalAlignment(JTextField.CENTER);
        jtf.setColumns(15);
        
        
        jp1.add(jl,BorderLayout.WEST);
        jp1.add(jtf,BorderLayout.CENTER);
        jp1.add(jl2,BorderLayout.EAST);
        jp2.setLayout(new FlowLayout());
               
        jp2.add(start);
        jp2.add(stop);
        jp2.add(clear);
        
        //定义布局
        jf.add(jp1,BorderLayout.CENTER);
        jf.add(jp2,BorderLayout.SOUTH);
         jf.setVisible(true);
       
        start.addActionListener(this);
        stop.addActionListener(this);
        clear.addActionListener(this);
    }
   //时间运行主要的部分,如果run为真,则时间每隔1ms递增,
     public void run() {
        float time = 0;
		jtf.setText(time2str(time));
        while (!this.isAlive() && !this.isInterrupted()) {
            if (Run) {//若可运行,则显示。每隔1ms时间递增。
                jtf.setText(time2str(time));
                //System.out.println(time2str(time));
                try {
                    Thread.sleep(100);//每隔1ms计时
                } catch (InterruptedException e1) {
                }
                time += 1;
            }
        }
    }
    //监听器事件,点击不同的按钮,则显示不同时间操作,点击按钮以后,判断运行标识发生变化,其他按钮
    //不可以点击,同时窗体的名称也发生变化。
     public void actionPerformed(ActionEvent e){ 
        String event=e.getActionCommand();
        if(event=="开始"){
       jf.setTitle("Start...");
            Run=true;
            start.setEnabled(false);
            stop.setEnabled(true);
            clear.setEnabled(false);
        }else if(event=="暂停"){          
            Run=false;
           jf.setTitle("Pause...");
            stop.setEnabled(false);
            start.setEnabled(true);
            clear.setEnabled(true);
        }else if(event=="清零"){
            time=0;
            Run=false;                
            jtf.setText(""+time);
            jf.setTitle("Clean...");//窗体的标题发生了改变
            start.setEnabled(true);
            stop.setEnabled(false);
            clear.setEnabled(false);
        }
    }
}

运行结果:在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值