swing+Thread实现倒计时(包含界面居中的方法)

老师代码

import java.awt.Font;   
import java.awt.GridLayout;   
import java.awt.Toolkit;   
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
  
import javax.swing.JButton;   
import javax.swing.JFrame;   
import javax.swing.JLabel;   
  
public class MyTimer extends JFrame implements Runnable, ActionListener{   
    private int hour;   
    private int minute;   
    private int second;   
    private JLabel timeLabel;   
       
    public MyTimer() {   
        this(2,0,0);   
    }   
       
    public MyTimer(int hour, int minute, int second) {   
        this.setTime(hour, minute, second);   
        this.setLayout(new GridLayout(1,2));   
        timeLabel = new JLabel();   
        timeLabel.setFont(new Font(Font.SERIF, Font.BOLD, 20));   
        this.setText();   
        this.add(timeLabel);   
        JButton btn = new JButton("开始");   
        btn.addActionListener(this);   
        this.add(btn);   
        this.pack();   
        this.setResizable(false);   
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        Toolkit tool = Toolkit.getDefaultToolkit();   
        double width = tool.getScreenSize().getWidth();   
        double height = tool.getScreenSize().getHeight();   
        this.setLocation((int)((width-this.getWidth())/2)   
                ,(int)((height-this.getHeight())/2));   
        this.setVisible(true);   
    }   
       
    private void setTime(int hour, int minute, int second) {   
        this.hour = hour;   
        this.minute = minute;   
        this.second = second;   
    }   
       
    private void setText() {   
        this.timeLabel.setText((this.hour<10?"0"+this.hour:this.hour)   
                +":"+(this.minute<10?"0"+this.minute:this.minute)   
                +":"+(this.second<10?"0"+this.second:this.second));   
    }   
       
    @Override  
    public void actionPerformed(ActionEvent event) {   
        ((JButton)event.getSource()).setEnabled(false);   
        new Thread(this).start();   
    }   
  
    public static void main(String[] args) {   
        new MyTimer(1,0,0);   
    }   
  
    public void run() {   
        while (true) {   
            try {   
                Thread.sleep(1000);   
            } catch (InterruptedException e) {   
                e.printStackTrace();   
            }   
            this.second--;   
            if (this.second<0) {   
                this.minute--;   
                this.second=59;   
            }   
            if (this.minute<0) {   
                this.hour--;   
                this.minute=59;   
            }   
            if (this.hour<0) {   
                break;   
            }   
            this.setText();   
        }   
           
    }   
       
}  

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值