java倒计时代码注解 java倒计时三种简单实现方式

写一个计时器 JAVA代码是什么?应该用线程里面的Timer来控制package com.sy.game.test;
import java.util.Timer;
import java.util.TimerTask;
public class TimeTask {
public static void main(String[] args) {
TimeTask tTask=new TimeTask();
tTask.timeVoid();
}
public void timeVoid(){
final Timer timer = new Timer();
TimerTask tt=new TimerTask() {
@Override
public void run() {
System.out.println("到点啦!");
timer.cancel();
}
};
timer.schedule(tt, 3000);
}
}
整合的:
/*
* java倒计时器
* shiyang
* */
package com.sy.game.test;
import java.awt.Container;
import java.awt.FlowLayout;
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.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
@SuppressWarnings("unused")
public class TimeController extends JFrame implements ActionListener {
private static final long serialVersionUID = 4603262282860990473L;
private static final int DEFAULT_WIDTH = 200;
private static final int DEFAULT_HEIGHT = 100;
private static final int width = Toolkit.getDefaultToolkit()
.getScreenSize().width;
private static final int height = Toolkit.getDefaultToolkit()
.getScreenSize().height;
private Container container;
private JButton btn;
private JTextField jtfTime;
private Timer tmr;
public TimeController() {
initComponents();
Timer tmr = new Timer(1000, this);
this.tmr = tmr;
setVisible(true);
}
private void initComponents() {
this.setTitle("SY秒表");
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation((width - DEFAULT_WIDTH) / 2,
(height - DEFAULT_HEIGHT) / 2);
jtfTime = new JTextField("10");
btn = new JButton("开始倒计时");
container = getContentPane();
JPanel panel = new JPanel();
panel.add(btn);
panel.add(jtfTime);
this.add(panel);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn) {
jtfTime.setText("10");
tmr.start();
} else {
int t;
t = Integer.parseInt(jtfTime.getText());
t--;
jtfTime.setText("" + t);
if (t = 0) {
tmr.stop();
}
}
}
public static void main(String[] args) {
TimeController timeController = new TimeController();
}
}
java 中 倒计时 一分钟 怎么写?代码如下:
public class TimeCountDown {
public static void countDown(int seconds) {
System.err.println("倒计时" + seconds + "秒,倒计时开始:");
int i = seconds;
while (i0) {
System.err.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i--;
}
System.err.println(i);
System.err.println("倒计时结束");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
countDown(60);// 倒计时1分钟
}
}
求JAVA 使用 Thread 和 Timer 类来做倒计时的程序代码抱歉,之前没看到第二个条件 , 重新写了下 。在本机上可以正确运行 。import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class TimeThreadFrame extends JFrame{ // 定义组件 private JLabel lblTime; private JTextField txtInput; private JButton btnEnter; // 记录所要启动的程序 private Process runningProcess;// 构造方法 public TimeThreadFrame(){ // 设置窗体的相关属性 super("TimerThread"); this.setSize(300,200); this.setLayout(null); this.setLocation(100,50); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建组件 this.lblTime = new JLabel("请输入倒计时时间"); this.lblTime.setBounds(30,20,200,30); this.txtInput = new JTextField(); this.txtInput.setBounds(30,70,100,30); this.btnEnter = new JButton("确定"); this.btnEnter.setBounds(150,70,70,30); this.runningProcess = null; // 给JTextField注册监听 this.txtInput.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent ke) { } public void keyReleased(KeyEvent ke) { } public void keyTyped(KeyEvent ke) { txtInput_KeyTyped(ke); } }); // 给JButton注册监听 this.btnEnter.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ btnEnter_ActionPerformed(ae); } }); // 将各组件添加到窗体上 add(lblTime); add(txtInput); add(btnEnter); // 显示窗体 this.setVisible(true); } // 输入时的事件处理,控制用户只能输入数字 public void txtInput_KeyTyped(KeyEvent ke){ if(ke.getKeyChar()'0' || ke.getKeyChar()'9'){ ke.setKeyChar('\0'); } } // 点击按钮时的事件处理 , 核心! public void btnEnter_ActionPerformed(ActionEvent ae){ // 获得用户输入的倒计时时间 String strTime = this.txtInput.getText(); if(strTime.equals("")){ // 检测用户是否输入 this.lblTime.setText("您尚未输入,请输入!"); } else{ Integer time = Integer.parseInt(strTime); // 创建线程 TimeThread tt = new TimeThread(this.lblTime,time); tt.start(); // 创建Timer Timer timer = new Timer(); timer.schedule(new TimerTask(){ // 启动其他程序 public void run() { try { // 当程序不存在时,会进行创建;存在时直接调用 。runningProcess = Runtime.getRuntime().exec("D:\\Program Files\\Tencent\\QQDoctor\\QQDoctor.exe"); } catch (IOException e) { e.printStackTrace(); }}}, time * 1000); } } // 启动窗体 public static void main(String[] args){ TimeThreadFrame ttf = new TimeThreadFrame(); } } // 时间线程类 class TimeThread extends Thread{ private JLabel lblTime; private int time; // 构造方法传入,显示事件的JLabel和倒计时的时间 。public TimeThread(JLabel lblTime, int time){ this.lblTime = lblTime; this.time = time; } // run方法 public void run(){ while(time0){ // 显示所剩时间 this.lblTime.setText("所剩时间:" + time); // 所剩时间减少 time--; try { this.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }

推荐阅读