抱歉,之前沒看到第二個(gè)條件,重新寫了下。 在本機(jī)上可以正確運(yùn)行。 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; // 記錄所要啟動(dòng)的程序 private Process runningProcess; // 構(gòu)造方法 public TimeThreadFrame(){ // 設(shè)置窗體的相關(guān)屬性 super("TimerThread"); this.setSize(300,200); this.setLayout(null); this.setLocation(100,50); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 創(chuàng)建組件 this.lblTime = new JLabel("請(qǐng)輸入倒計(jì)時(shí)時(shí)間"); 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注冊(cè)監(jiān)聽 this.txtInput.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent ke) { } public void keyReleased(KeyEvent ke) { } public void keyTyped(KeyEvent ke) { txtInput_KeyTyped(ke); } }); // 給JButton注冊(cè)監(jiān)聽 this.btnEnter.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ btnEnter_ActionPerformed(ae); } }); // 將各組件添加到窗體上 add(lblTime); add(txtInput); add(btnEnter); // 顯示窗體 this.setVisible(true); } // 輸入時(shí)的事件處理,控制用戶只能輸入數(shù)字 public void txtInput_KeyTyped(KeyEvent ke){ if(ke.getKeyChar() '0' || ke.getKeyChar() '9'){ ke.setKeyChar('\0'); } } // 點(diǎn)擊按鈕時(shí)的事件處理,核心! public void btnEnter_ActionPerformed(ActionEvent ae){ // 獲得用戶輸入的倒計(jì)時(shí)時(shí)間 String strTime = this.txtInput.getText(); if(strTime.equals("")){ // 檢測(cè)用戶是否輸入 this.lblTime.setText("您尚未輸入,請(qǐng)輸入!"); } else{ Integer time = Integer.parseInt(strTime); // 創(chuàng)建線程 TimeThread tt = new TimeThread(this.lblTime,time); tt.start(); // 創(chuàng)建Timer Timer timer = new Timer(); timer.schedule(new TimerTask(){ // 啟動(dòng)其他程序 public void run() { try { // 當(dāng)程序不存在時(shí),會(huì)進(jìn)行創(chuàng)建;存在時(shí)直接調(diào)用。 runningProcess = Runtime.getRuntime().exec("D:\\Program Files\\Tencent\\QQDoctor\\QQDoctor.exe"); } catch (IOException e) { e.printStackTrace(); } } }, time * 1000); } } // 啟動(dòng)窗體 public static void main(String[] args){ TimeThreadFrame ttf = new TimeThreadFrame(); } } // 時(shí)間線程類 class TimeThread extends Thread{ private JLabel lblTime; private int time; // 構(gòu)造方法傳入,顯示事件的JLabel和倒計(jì)時(shí)的時(shí)間。 public TimeThread(JLabel lblTime, int time){ this.lblTime = lblTime; this.time = time; } // run方法 public void run(){ while(time 0){ // 顯示所剩時(shí)間 this.lblTime.setText("所剩時(shí)間:" + time); // 所剩時(shí)間減少 time--; try { this.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),石峰企業(yè)網(wǎng)站建設(shè),石峰品牌網(wǎng)站建設(shè),網(wǎng)站定制,石峰網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,石峰網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
基于控制臺(tái)的話很簡(jiǎn)單的,我跟你說一下大體思路吧,二話不說先來個(gè)for循環(huán),然后輸出倒計(jì)時(shí)的數(shù)字,程序睡一秒,在輸出倒計(jì)時(shí)數(shù)字,如此循環(huán),簡(jiǎn)單吧,下面看程序:
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("爆炸");
}
其他基于網(wǎng)頁的還是基于用戶界面都可以使用這個(gè)思路的
參考一下吧。
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("結(jié)束");
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();
}
}
}
};
}
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("計(jì)時(shí)器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("計(jì)時(shí)器"); 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);// 設(shè)定 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(); } } } }}
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;
// 構(gòu)造方法
public TimeThreadFrame(){
// 設(shè)置窗體的相關(guān)屬性
super("TimerThread");
this.setSize(300,200);
this.setLayout(null);
this.setLocation(100,50);
// 創(chuàng)建組件
this.lblTime = new JLabel("請(qǐng)輸入倒計(jì)時(shí)時(shí)間");
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注冊(cè)監(jiān)聽
this.txtInput.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent ke) {
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
txtInput_KeyTyped(ke);
}
});
// 給JButton注冊(cè)監(jiān)聽
this.btnEnter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnEnter_ActionPerformed(ae);
}
});
// 將各組件添加到窗體上
add(lblTime);
add(txtInput);
add(btnEnter);
// 顯示窗體
this.setVisible(true);
}
// 輸入時(shí)的事件處理,控制用戶只能輸入數(shù)字
public void txtInput_KeyTyped(KeyEvent ke){
if(ke.getKeyChar() '0' || ke.getKeyChar() '9'){
ke.setKeyChar('\0');
}
}
// 點(diǎn)擊按鈕時(shí)的事件處理,核心!
public void btnEnter_ActionPerformed(ActionEvent ae){
// 獲得用戶輸入的倒計(jì)時(shí)時(shí)間
String strTime = this.txtInput.getText();
if(strTime.equals("")){
// 檢測(cè)用戶是否輸入
this.lblTime.setText("您尚未輸入,請(qǐng)輸入!");
}
else{
Integer time = Integer.parseInt(strTime);
// 創(chuàng)建線程
TimeThread tt = new TimeThread(this.lblTime,time);
tt.start();
// 創(chuàng)建Timer
Timer timer = new Timer();
timer.schedule(new TimerTask(){
// 啟動(dòng)其他程序
public void run() {
System.out.print("ok");
}
}, time * 1000);
}
}
// 啟動(dòng)窗體
public static void main(String[] args){
new TimeThreadFrame();
}
}
// 時(shí)間線程類
class TimeThread extends Thread{
private JLabel lblTime;
private int time;
// 構(gòu)造方法傳入,顯示事件的JLabel和倒計(jì)時(shí)的時(shí)間。
public TimeThread(JLabel lblTime, int time){
this.lblTime = lblTime;
this.time = time;
}
// run方法
public void run(){
while(time 0){
// 顯示所剩時(shí)間
this.lblTime.setText("所剩時(shí)間:" + time);
// 所剩時(shí)間減少
time--;
try {
this.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
名稱欄目:java時(shí)間倒計(jì)時(shí)代碼 用java編寫一個(gè)倒計(jì)時(shí)程序
分享鏈接:http://aaarwkj.com/article10/docphdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、品牌網(wǎng)站建設(shè)、微信公眾號(hào)、Google、營銷型網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)