实验程序如下:

package TimeToEnded;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class time extends JFrame
{
    JButton butt1;
    JLabel la1;
    public time()
{
    butt1=new JButton("奥运会倒计时");
    butt1.setFont(new Font("宋体",2,30));
    butt1.addActionListener(new ActionListener()
{
        @Override
    public void actionPerformed(ActionEvent arg0)
    {
        new time();
    }
    });
    la1=new JLabel();
    Thread tr1= new Thread()
    {
    public void run()
    {
      int day=100;//100天倒计时
      int hour,min,second;
    while(day>=0)
    {
      if(day==0)
        day=0;
      else --day;
      hour=24;
       while(hour>=0)
        {
         if(hour==0)
                hour=0;
         else --hour;
         min=60;
          while(min>=0)
          {
            if(min==0)
                min=0;
            else
                min--;
            second=60;
              while(second>0)
               {
                 second--;
                 if(day+min+hour+second==0)
                        second=0;
                 if(day==0&&hour==0&&min<3)
                   {
                    la1.setForeground(Color.black);//红色背景
                     }
                 la1.setFont(new Font("黑体",1,33));//黑体、加粗、字号
                 la1.setText(day+"天"+hour+"小时"+min+"分钟"+second+"秒");
                  try
                    {
                      Thread.sleep(1000);//线程睡眠,单位毫秒 1s=1000ms
                      }
                    catch (InterruptedException e)
                    {
                    e.printStackTrace();
                      }
                        }
                     }
                }
            }
        }
    };
    this.setTitle("奥运会倒计时100天");
    this.setSize(500, 240);
    this.add(butt1,BorderLayout.NORTH);
    this.add(la1,BorderLayout.CENTER);
    tr1.start();
    this.setResizable(true);
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(String[] args)
    {
        new time();
    }
}
 
posted on 2019-06-30 21:12  买门票  阅读(105)  评论(1编辑  收藏  举报