java时间倒计时代码 用java编写一个倒计时程序
求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(time 0){ // 显示所剩时间 this.lblTime.setText("所剩时间:" + time); // 所剩时间减少 time--; try { this.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
成都创新互联公司是一家集网站建设,石峰企业网站建设,石峰品牌网站建设,网站定制,石峰网站建设报价,网络营销,网络优化,石峰网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
怎么编写一个倒计时的java的程序?求具体步骤!
基于控制台的话很简单的,我跟你说一下大体思路吧,二话不说先来个for循环,然后输出倒计时的数字,程序睡一秒,在输出倒计时数字,如此循环,简单吧,下面看程序:
public static void main(String[] args) {
for(int i=10;i0;i--){
System.out.print(i+" ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.err.print("爆炸");
}
其他基于网页的还是基于用户界面都可以使用这个思路的
java连连看里设置150秒倒计时,重行开始游戏时时间再次从150秒开始倒计时的代码怎么写,急!!!!!!!
参考一下吧。
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Program {
static int seconds = 150;
private TimeThread tt = null;
private boolean ttFlag = false;
private void init() {
final JLabel tip = new JLabel();
final JButton start = new JButton("开始");
final JButton end = new JButton("结束");
JFrame f = new JFrame();
f.setLayout(new FlowLayout(5));
f.add(tip);
f.add(start);
f.add(end);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(300, 150);
f.setLocationRelativeTo(null);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start.setEnabled(false);
tip.setFont(new Font("宋体",Font.BOLD,27));
ttFlag = true;
tt = new TimeThread(tip);
tt.start();
}
});
end.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start.setEnabled(true);
tip.setText("");
Program.seconds = 150;
ttFlag = false;
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
new Program().init();
}
class TimeThread extends Thread {
private JLabel tip;
TimeThread(JLabel tip) {
this.tip = tip;
}
@Override
public void run() {
int seconds = Program.seconds;
tip.setText(seconds+"");
while (seconds-- 0 ttFlag) {
tip.setText(seconds+"");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
用java编写一个倒计时器代码。
import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class TimerDemo extends JFrame implements ActionListener { private static final long serialVersionUID = 201306211111L; private JTextField screen = new JTextField("0"); private JButton start = new JButton("开始"); private JButton reset = new JButton("重置"); private JPanel panel = new JPanel(); private boolean isRunning; private int time; private int timeBetween; public TimerDemo(int timeBetween) { super("计时器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("计时器"); this.timeBetween = 100; try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() { panel.setLayout(new GridLayout()); panel.add(start); panel.add(reset); start.addActionListener(this); reset.addActionListener(this); screen.setFont(new Font("幼圆", Font.BOLD, 60)); screen.setHorizontalAlignment(JTextField.CENTER); screen.setEditable(false); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.SOUTH); c.add(screen, BorderLayout.CENTER); this.setSize(200, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new TimerDemo(1);// 设定 1ms/次 // new TimerDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { if (start.getText().equals("开始")) { start.setText("暂停"); isRunning = true; } else if (start.getText().equals("暂停")) { start.setText("开始"); isRunning = false; } } if (e.getSource() == reset) { start.setText("开始"); screen.setText("0"); isRunning = false; time = 0; } new Thread(new TimeZone()).start(); } class TimeZone implements Runnable { @Override public void run() { while (isRunning) { time++; if (time = Integer.MAX_VALUE) { screen.setText("ERROR"); JOptionPane.showMessageDialog(null, "ERROR"); isRunning = false; } screen.setText(String.valueOf(time)); try { Thread.sleep(timeBetween); } catch (Exception e) { e.printStackTrace(); } } } }}
java 设计一个简单的倒计时
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
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;
// 构造方法
public TimeThreadFrame(){
// 设置窗体的相关属性
super("TimerThread");
this.setSize(300,200);
this.setLayout(null);
this.setLocation(100,50);
// 创建组件
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);
// 给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() {
System.out.print("ok");
}
}, time * 1000);
}
}
// 启动窗体
public static void main(String[] args){
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(time 0){
// 显示所剩时间
this.lblTime.setText("所剩时间:" + time);
// 所剩时间减少
time--;
try {
this.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
网页标题:java时间倒计时代码 用java编写一个倒计时程序
网页URL:http://myzitong.com/article/docphdo.html