实验十四:线程设计

本文介绍了一个使用Java实现的倒计时程序,通过Swing框架创建GUI界面,并利用Thread.sleep()方法进行时间更新,展示从一年的时间逐渐递减到0的过程。

实验代码:

package 实验14;
 
import java.awt.FlowLayout;
import java.awt.Font;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
class TIME extends JFrame{
     
    int day =365;
    int seconds=day*24*60*60;
    private JLabel text;
    public void run() {      
    while (seconds > 0) {
        seconds--;
        int days=seconds/60/60/24;
        int hours= seconds/60/60%24;
        int minutes= seconds/60%60;
        int second=seconds%60;
       System.out.println(days+"天"+hours+"时"+minutes+"分"+second+"秒");     
       try {
            this.text.setText(days+"天"+hours+"时"+minutes+"分"+second+"秒");
            Thread.sleep(1000);
                }
       catch (InterruptedException e) {
                    e.printStackTrace();
                }      
          }}
        public TIME(){
        this.setLayout(new FlowLayout());  
        this.setTitle("Introduction");
        this.setSize(320,200);
        this.setLocation(300,240);
        this.add(new JLabel("距离毕业还剩")).setFont(new Font("黑体", 1, 20));    
        this.text=new JLabel("365天00时00分00秒");
        this.add(text).setFont(new Font("黑体", 1, 30));
         
        this.setVisible(true);}
      }
     
 
public class Shiyan14 {
     
    public static void main(String[] args)
    {
            new TIME().run();    
            }
}

实验结果:

 

转载于:https://www.cnblogs.com/itsRes/p/11111100.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值