zoukankan      html  css  js  c++  java
  • 实验十四———线程设计倒计时100天

      问题描述:

        用线程设计,制作数字化倒计时,计时单位可用秒、分和天,例如 奥运会100天倒计时天

      效果预测

     

      代码粘贴

     1 package TimeToEnded;
     2 import java.awt.*;
     3 import java.awt.event.ActionEvent;
     4 import java.awt.event.ActionListener;
     5 import javax.swing.*;
     6 public class tiameDown extends JFrame//继承JFrame 类
     7 {
     8     JButton butt1;
     9     JLabel la1;
    10 
    11     public tiameDown()
    12 {
    13     butt1=new JButton("倒计时Renew");
    14     butt1.setFont(new Font("宋体",2,30));//宋体、斜体、字号
    15     //为JButton添加监听,实现点击时重新开始
    16     butt1.addActionListener(new ActionListener()
    17 {
    18         @Override
    19     public void actionPerformed(ActionEvent arg0)
    20     {
    21     //dispose();//关闭当前窗口
    22         new tiameDown();//新建一个窗口
    23     }
    24 
    25     });
    26     //匿名创建一个线程类Thread来实现时间倒计时
    27     la1=new JLabel();//标签,待用
    28     Thread tr1= new Thread() //匿名创建,毋须实例对象
    29     {
    30     public void run()  //线程运行方法,实现Runnable()接口
    31     {
    32       int day=100;//100天倒计时
    33       int hour,min,second;
    34     while(day>=0)
    35     {
    36       if(day==0)
    37         day=0;
    38       else --day;
    39       hour=24;
    40        while(hour>=0)
    41         {
    42          if(hour==0)
    43                 hour=0;
    44          else --hour;
    45          min=60;
    46           while(min>=0)
    47           {
    48             if(min==0)
    49                 min=0;
    50             else
    51                 min--;
    52             second=60;
    53               while(second>0)
    54                {
    55                  second--;
    56                  if(day+min+hour+second==0)
    57                         second=0;
    58                  if(day==0&&hour==0&&min<3)
    59                    {
    60                      //当时间只剩3min时闪红
    61                     la1.setForeground(Color.RED);//红色背景
    62                      }
    63                  la1.setFont(new Font("黑体",1,33));//黑体、加粗、字号
    64                  la1.setText(day+"天"+hour+"小时"+min+"分钟"+second+"秒");
    65                   try
    66                     {
    67                       Thread.sleep(1000);//线程睡眠,单位毫秒 1s=1000ms
    68                       }
    69                     catch (InterruptedException e)
    70                     {
    71                     e.printStackTrace();
    72                       }
    73                         }
    74                      }
    75                 }
    76             }
    77         }
    78     };
    79     this.setTitle("奥运会100Days倒计时");
    80     this.setSize(500, 240);//长、宽
    81     this.add(butt1,BorderLayout.NORTH);//边界布局 北 即上
    82     this.add(la1,BorderLayout.CENTER);//中间
    83     tr1.start();//线程开始启动
    84     this.setResizable(true);
    85     this.setVisible(true);
    86     this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    87     }
    88 
    89     public static void main(String[] args)
    90     {
    91         new tiameDown();
    92 
    93     }
    94 
    95 }

      心得体会

         加载线程:

          1.用Thread类的run()方法实现Runnable接口    

          2.直接用Runnab接口的run方法实现

             启动线程:使用start()方法

           对于时间倒计时,以上程序还有不足

    2019-06-29

    <      以上,完成!      >    

    @勤奋的lu3
  • 相关阅读:
    前Citrix技术总监:虚拟化将解决云计算安全问题 狼人:
    悲剧:金山毒霸官网被黑客攻破 狼人:
    接口程序设计Windows CE嵌入式系统程序开发
    参数返回Oracle 常用函数:nvl/nullif/case when/wm_concat/replace
    数据恢复[Oracle] Flashback闪回机制
    重复字段Oracle删除重复行
    进程函数一步步理解Linux之信号
    读取数据事务
    菜单函数Android学习整理菜单Menu
    参数实现Oracle SQL中实现indexOf和lastIndexOf功能
  • 原文地址:https://www.cnblogs.com/lul3/p/11083797.html
Copyright © 2011-2022 走看看