java--计时器

计时器

一、窗口化

public class Pro extends JFrame{

private JTextField textField = new JTextField(45);//系统时间文本框
private JTextField textField2 = new JTextField(45);//倒计时文本框
int t1=0,x=1;
private boolean TT = true;

public Pro()throws InterruptedException{
setTitle("计时器"); //标题
setLayout(null); //取消布局管理器设置
setBounds(100,120,450,450);
Container c = getContentPane(); //容器对象
JButton b1 = new JButton("开始");
b1.setBounds(180,130,80,30);
JButton b2 = new JButton("暂停");
b2.setBounds(180,185,80,30);
JButton b8 = new JButton("清零");
b8.setBounds(180,240,80,30);
c.add(b1);c.add(b2);c.add(b8);//增加控件
setVisible(true);//窗口可视化
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//结束窗口所在的应用程序

textField.setFont(new Font("宋体",Font.BOLD,60));//改变数字的大小
textField.setBackground(Color.white);//改变文本框的颜色
textField.setBounds(10,10,400,90);//改变文本框的大小
textField.setEditable(false);//控件不能编辑
add(textField);//增加文本框,显示系统时间

textField2.setFont(new Font("宋代",Font.BOLD,60));
textField2.setBackground(Color.white);
textField2.setBounds(10,280,400,90);
textField2.setEditable(false);
add(textField2);//增加文本框,显示计时

}

public static void main(String[] args)throws InterruptedException {
new Pro();
}

}


 

二、全部代码

  1 import java.text.SimpleDateFormat;
  2 import java.util.*;
  3 import java.util.concurrent.TimeUnit;
  4 import java.text.*;
  5 import java.awt.*;  
  6 import java.awt.event.ActionEvent;
  7 import java.awt.event.ActionListener;
  8 import java.sql.*;
  9 import javax.swing.*;  
 10 import java.util.Date;
 11 import java.text.SimpleDateFormat;
 12 
 13 public class Pro extends JFrame{
 14     
 15     private JTextField textField = new JTextField(45);//系统时间文本框
 16     private JTextField textField2 = new JTextField(45);//倒计时文本框
 17     int t1=0,x=1;
 18     private boolean TT = true; 
 19         
 20     public  Pro()throws InterruptedException{
 21         setTitle("计时器");  //标题
 22         setLayout(null);  //取消布局管理器设置
 23         setBounds(100,120,450,450);
 24         Container c = getContentPane();  //容器对象
 25         JButton b1 = new JButton("开始");
 26         b1.setBounds(180,130,80,30);
 27         JButton b2 = new JButton("暂停");
 28         b2.setBounds(180,185,80,30);
 29         JButton b8 = new JButton("清零");
 30         b8.setBounds(180,240,80,30);
 31         c.add(b1);c.add(b2);c.add(b8);//增加控件
 32         setVisible(true);//窗口可视化
 33         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//结束窗口所在的应用程序
 34         
 35         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
 36 
 37         textField.setFont(new Font("宋体",Font.BOLD,60));//改变数字的大小
 38         textField.setBackground(Color.white);//改变文本框的颜色
 39         textField.setBounds(10,10,400,90);//改变文本框的大小
 40         textField.setEditable(false);//控件不能编辑
 41         add(textField);//增加文本框,显示系统时间
 42         textField.setText("   "+df.format(new Date()));//获系统时间
 43         
 44         textField2.setFont(new Font("宋代",Font.BOLD,60));
 45         textField2.setBackground(Color.white);
 46         textField2.setBounds(10,280,400,90);
 47         textField2.setEditable(false);
 48         add(textField2);//增加文本框,显示计时
 49         textField2.setText("   计时:"+t1);
 50             
 51         //获取系统时间时动态显示,线程
 52          Display timedisplay = new Display();
 53          timedisplay.start() ;
 54         
 55        //添加一个action监听,确定键
 56           b1.addActionListener(new ActionListener(){    
 57                   public void actionPerformed(ActionEvent e){                    
 58                    //计时开始
 59                    Dis tt = new Dis();
 60                    tt.start();
 61                   }
 62           });
 63           
 64         //添加一个action监听,暂停键
 65           b2.addActionListener(new ActionListener(){
 66               public void actionPerformed(ActionEvent e){                 
 67                  Dis tt2 = new Dis();
 68                 tt2.sto();
 69               }
 70           });
 71           
 72         //添加一个action监听,清零键
 73           b8.addActionListener(new ActionListener(){
 74                public void actionPerformed(ActionEvent e){
 75                     textField2.setText("   计时:"+"0");
 76                     t1=0;
 77                }
 78            });
 79     }
 80     
 81         
 82     //显示系统时间
 83     private class Display extends Thread {
 84         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");//系统时间以时:分:秒显示
 85         public Display() { 
 86             
 87         }         
 88         @Override
 89         public void run(){
 90             while(true){              
 91                 textField.setText("   "+sdf.format(new Date()));
 92                 try {
 93                     Thread.sleep(1000) ;//迟顿一秒
 94                 } catch (InterruptedException e) {
 95                     e.printStackTrace();
 96                 }
 97             }
 98         }
 99     }
100     
101     //计时开始
102     private class Dis extends Thread{
103         public Dis(){    
104             
105         }
106         public void sto(){
107             TT=false;
108         }
109         public void run(){
110             TT=true;
111             while(TT){
112                 t1++;
113             textField2.setText("   计时:"+t1);
114              try {
115                  Thread.sleep(1000) ;
116              } catch (InterruptedException e) {
117                  e.printStackTrace();
118              }
119             }
120         }
121         
122     }
123  
124  
125     
126     public static void main(String[] args)throws InterruptedException {  
127        new Pro();     
128     }    
129 }
130    
131     

 

三、效果图

 

 

转载于:https://www.cnblogs.com/zcjd/p/8627041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值