使用线程来实现一个时间倒计时
来源:互联网 发布:网络人身攻击怎么办 编辑:程序博客网 时间:2024/06/11 14:39
package TestCountDown;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class CountDown extends JFrame { JButton jButton; JLabel jLabel; int time=60; public CountDown() { FlowLayout fl=new FlowLayout(FlowLayout.CENTER); this.setLayout(fl); //为按钮jButton添加监听器,实现点击时倒计时重新开始 jButton=new JButton("重新开始"); jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { dispose();//关闭当前窗口 new CountDown();//新建一个窗口 } }); //匿名创建一个线程内部类来实现时间倒计时,这是整篇代码的核心 jLabel=new JLabel(); new Thread(){ public void run() { while(time>0) { time--; if(time<6) {//当时间只剩5秒时闪红 jLabel.setForeground(Color.RED); } jLabel.setText(time+"秒"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); this.add(jButton); this.add(jLabel); this.setTitle("倒计时"); this.setSize(300, 200); this.setResizable(true); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { new CountDown(); }}
效果如下
阅读全文
0 0
- 使用线程来实现一个时间倒计时
- js 简单实现一个时间倒计时计数器
- iOS --- 使用UIImageView来实现倒计时动画
- 不用线程实现倒计时
- JavaScript 实现使用时间差来转换倒计时效果。
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- 使用GCD实现倒计时
- 使用CountDownTimer实现倒计时
- 使用RxJava实现倒计时
- jquery.countdown.js一个时间倒计时的插件
- Js实现一个时间显示器
- Android自定义一个时间轴,通过ListView来实现时间轴的效果
- 用线程实现C#中的倒计时
- JDBC | 3
- 测试js程序运行时间
- TCP/IP协议层级个人笔记
- 【2017.12.02】C组比赛总结
- LeetCode-27. Remove Element
- 使用线程来实现一个时间倒计时
- 读书笔记_MySQL必知必会
- 《kubernetes-1.8.0》15-addon-vSphere Cloud Provider
- LintCode-第一题:A+B problem (不用“+”等运算符)(位运算实现加法)
- Windows程序设计:直线绘制
- linux下yum使用本地光盘做为安装源
- 【Hive】hive-2.1.1内嵌模式安装配置全过程
- 集合类
- python数据类型