首頁 > Java > java教程 > 主體

Java倒數計時器

王林
發布: 2024-08-30 15:53:13
原創
869 人瀏覽過

在Java中,有幾種情況會將倒數計時器等時間敏感任務加入Java表單中。在這些計時器中,使用者可以設定觸發功能之前的時間量。在需要連續觸發該功能的情況下,它將持續運作。當達到倒數計時時間時,計時器可以重設。 Java 的內建套件可用於設定時間量並定期執行某些操作。所有這些都可以根據使用者的要求進行更改。本文檔讓我們了解如何在 Java 中設定倒數計時器。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Java 中 CountDownTimer 的聲明

以下是Java中倒數計時器的聲明。

public abstract class CountDownTimer extends Object
登入後複製

建構子

倒數計時器有一個建構函數,如下所示。

CountDownTimer (long millisInFuture, long CountDownInterval)
登入後複製
  • millisInFuture:此參數表示從呼叫 start() 方法到呼叫 onFinish() 方法之間的未來毫秒數。
  • countDownInterval:取得 onTick() 回呼的時間間隔。

Java倒數計時器的方法

下面提到了不同的方法:

1.取消

public final void cancel ()
登入後複製

定義:此方法有助於取消倒數計時。

2.完成式

public final void onFinish ()
登入後複製

定義:此方法有助於在時間到時回調。

3. onTick

public abstract void onTick ()
登入後複製

定義:此方法有助於定期回調。

4.開始

public final CountDownTimer start()
登入後複製

定義:此方法有助於開始倒數。

Java 中倒數時間如何運作?

以下是在Java中執行倒數計時器的步驟。

1.開啟要建立 Java 檔案的 IDE。 IDE 可以是 Eclipse、Netbeans 或 JBuilder X,這取決於使用者的需求。

2.導入包。在 Java 主文件頂部匯入所有所需的時間類別。

3.設定倒數時間。

4.倒數計時以毫秒為單位。因此,請確保變數也以毫秒為單位。如果你想將定時器設定為5秒,則必須提到“5000”,如下所示。

int cntdwn = 5000;
登入後複製

5.設定計時器的實例。

timer = new Timer(cntdwn, this);
登入後複製

6.使用 start() 方法啟動計時器。

timer.start();
登入後複製

7.儲存您建立的 Java 檔案。

8.編譯程式碼並按「執行」。

Java 倒數計時器範例

可以透過某些方式設定倒數計時器。讓我們看看如何借助 Java 程式語言來實現它們。

範例#1

Java 設定定時器的程式

代碼:

import java.util.Timer;
import java.util.TimerTask;
//sample class
public class CountDownTimerExample {
//declare timer t
Timer t;
//constructor of the class
public CountDownTimerExample(int seconds) {
t = new Timer();
//schedule the timer
t.schedule(new rt(), seconds*1000);
}
//sub class that extends TimerTask
class rt extends TimerTask {
//task to perform on executing the program
public void run() {
System.out.println("Seconds you have input is over..!!! ");
t.cancel(); //stop the thread of timer
}
}
//main method
public static void main(String args[]) {
//pass 5 seconds as timer
new CountDownTimerExample(5);
System.out.println("Count down starts now!!! ");
}
}
登入後複製

輸出:

執行程式碼時,將顯示以下訊息。

Java倒數計時器

倒數停止後,下方會顯示一則訊息,表示時間已設定為倒數結束。

Java倒數計時器

範例#2

Java中設定倒數計時器的另一種方法

代碼:

//Java program to create a count down timer
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
//class
public class CountDownTimerExample {
//declare the interval i and timer t
static int i;
static Timer t;
//main method
public static void main(String[] args) {
//create object for scanner
Scanner <u>sc</u> = new Scanner(System.in);
// input the seconds you want to count down
System.out.print("Enter the seconds you want to count down : ");
//save the seconds that is input in to the variable <u>sec</u>
String sec = sc.nextLine();
//set delay and period as 1000
int del = 1000;
int per = 1000;
t = new Timer();
i = Integer.parseInt(sec);
System.out.println(sec);
//performs the <u>specifiedd</u> task at certain intervals
t.scheduleAtFixedRate(new TimerTask()
{
//task to be performed
public void run()
{
System.out.println(seti());
}
}, del, per);
}
//set interval
private static final int seti() {
//if interval is 1, cancel
if (i == 1)
t.cancel();
return --i;
}
}
登入後複製

輸出:

在此程式中,使用者將被要求輸入倒數計時的秒數。

Java倒數計時器

輸入秒數後,我們將能夠看到倒數計時正在顯示。

Java倒數計時器

我們可以看到每秒顯示3,2,1,0。

範例 #3

程式建立一個倒數計時器,每秒顯示一則訊息。

代碼:

import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
public class CountDownTimerExample {
//declare tk and t
Toolkit tk;
Timer t;
//constructor of CountDownTimerExample class
public CountDownTimerExample() {
tk = Toolkit.getDefaultToolkit();
t = new Timer();
//initial delay and subsequent rate
t.schedule(new rt(), 0, 1*1000);
}
class rt extends TimerTask {
//declare a variable beep
int beep = 3;
//task to be performed
public void run() {
//if BEEP VARIABLE IS GREATER THAN ZERO
if (beep > 0) {
//perform beep operation and print after each second
tk.beep();
System.out.println("One second over . . . Beep!");
//decrement the value beep
beep--;
}
//if beep variable is less than zero
else {
tk.beep();
System.out.println("The Time's over. . .!");
//AWT thread stops
System.exit(0);
}
}
}
public static void main(String args[]) {
System.out.println("Task is going to start. . .");
new CountDownTimerExample();
System.out.println("Task that is set up is scheduled. . .");
}
}
登入後複製

輸出:

可以看到,在執行程式碼時,會顯示兩個訊息,例如「任務即將開始。 。」。和「已設定的任務已排程...」。之後,倒數計時開始。

Java倒數計時器

執行成功後,會列印「The Time’s over」訊息,表示倒數計時已到。

Java倒數計時器

結論

在 Java 中,時間敏感的任務是使用倒數計時器執行的。這些任務大部分是考試、遊戲、通知等。本文檔詳細討論了倒數計時器的詳細說明。

以上是Java倒數計時器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!