抱歉,之前沒看到第二個條件,重新寫了下。 在本機上可以正確運行。 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(); } } } }
創新互聯公司主要從事成都網站制作、做網站、外貿營銷網站建設、網頁設計、企業做網站、公司建網站等業務。立足成都服務建甌,十年網站建設經驗,價格優惠、服務專業,歡迎來電咨詢建站服務:13518219792
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(); } } } }}
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();
}
}
}
}
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倒計時程序源代碼是什么
鏈接分享:http://vcdvsql.cn/article4/dopjdie.html
成都網站建設公司_創新互聯,為您提供網站排名、服務器托管、建站公司、外貿網站建設、網站內鏈、品牌網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯