实验十四:线程设计

本文介绍了一个使用Java编写的奥运会倒计时程序,通过Swing实现了GUI界面,并利用多线程进行时间更新,展示了按钮、标签组件及线程操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验程序如下:

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();
    }
}
 

转载于:https://www.cnblogs.com/Y-CQ/p/11111242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值