黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java倒數(shù)計時器的介紹

Java倒數(shù)計時器的介紹

更新時間:2022-09-28 15:42:01 來源:動力節(jié)點 瀏覽2573次

在Java中,有幾種情況對時間敏感的任務,比如倒計時定時器被添加到Java形式。在這些計時器的時間,直到一個函數(shù)可以設置由用戶觸發(fā)。它將持續(xù)運行的情況下必須連續(xù)觸發(fā)的函數(shù)。當達到計數(shù)停機時,可以重置計時器。內(nèi)置的Java包可用于設置的時間和定期執(zhí)行某些操作。所有這些可以改變基于用戶的要求。本文讓我們看到倒數(shù)計時器可以在Java集合

用Java CountDownTimer宣言

以下是Java中倒數(shù)計時器的聲明。

public abstract class CountDownTimer extends Object

構造函數(shù)

倒數(shù)計時器有一個構造函數(shù),如下描述。

CountDownTimer (long millisInFuture, long CountDownInterval)

millisInFuture:這個參數(shù)提到,米爾斯在未來的數(shù)當調(diào)用start()方法,直到onFinish()方法被調(diào)用。

countDownInterval:間隔onTick()回調(diào)。

Java倒數(shù)計時器的方法

下面提到的不同的方法:

1. 取消

public final void cancel ()

定義:這種方法有助于取消倒計時。

2. OnFinish

public final void onFinish ()

定義:這種方法有助于回調(diào)的時候。

3.onTick

public abstract void onTick ()

定義:這種方法有助于在定期的回調(diào)。

4. 開始

public final CountDownTimer start()

定義:這種方法有助于開始倒計時。

在Java中倒計時時間是如何工作的?

下面是步驟執(zhí)行Java的倒數(shù)計時器。

1. 開放的IDE,您想要創(chuàng)建一個Java文件。Eclipse IDE可以、Netbeans或JBuilder X,這取決于用戶的需求。

2. 導入包。主要進口所需的所有的時間在您的Java類文件的頂部。

3.設置倒計時時間。

4. 倒計時發(fā)生在毫秒。因此,確保變量也以毫秒為單位。如果你想計時器設置為5秒,“5000”必須提到,如下所示。

int cntdwn = 5000;

5. 設置定時器的一個實例。

timer = new Timer(cntdwn, this);

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

timer.start();

7. 您已經(jīng)創(chuàng)建了保存Java文件。

8. 編譯代碼和按下運行。

Java倒數(shù)計時器的例子

有一些方式可以設置倒計時定時器。讓我們來看看如何實現(xiàn)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!!! ");
}
}

輸出:

在執(zhí)行代碼,將顯示以下消息。

倒數(shù)計時器停止后,下面將顯示一條消息,表明時間設置為倒計時結束了。

例# 2

設置一個倒數(shù)計時器在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 sc = 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 sec
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 specifiedd 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;
}
}

輸出:

在這個程序中,用戶將被要求輸入的秒倒計時。

進入秒之后,我們將能夠看到倒計時顯示。

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

示例# 3

項目創(chuàng)建一個倒數(shù)計時器,每秒鐘后顯示一條消息。

代碼:

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. . .");
}
}

輸出:

可以看出,在執(zhí)行代碼,兩條消息顯示,如“任務開始。”和“任務設置計劃…”。在那之后,倒計時開始。

以上就是關于“Java倒數(shù)計時器的介紹”,大家如果想了解更多相關知識,可以關注一下動力節(jié)點的Java視頻教程,里面的課程內(nèi)容細致全面,通俗易懂,適合小白學習,希望對大家能夠有所幫助哦。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 亚洲人成网址在线播放a | 国产满18av精品免费观看视频 | 午夜小影院 | 国产专区一va亚洲v天堂 | 国外欧美一区另类中文字幕 | 18av黄动漫网站在线观看 | 久久精品国产欧美日韩亚洲 | 一区二区三区日韩 | 美女黄网站 | 91在线视频免费播放 | 午夜色大片 | 成年人的天堂 | 91精品全国免费观看 | 黄色一级生活片 | 亚洲国产日韩欧美 | 亚洲第二页 | 国产11一12周岁女毛片 | 亚洲精品天堂在线观看 | 国产一级毛片午夜福 | 51av视频| 天堂资源在线中文 | 伊人国产在线播放 | 美女黄18 | 99热com | 噜噜噜天天躁狠狠躁夜夜精品 | 女黄人东京手机福利视频 | 亚洲欧美日韩中文v在线 | 全黄大全大色全免费大片 | 国产一区二三区 | 天天色天天操天天 | 亚洲欧美日韩在线一区 | 午夜色视频在线观看 | 日韩高清一区二区 | 午夜黄色| 成年黄网站色大免费全看 | 亚洲日本中文字幕天天更新 | 色播日韩 | 91欧美亚洲 | 日韩欧美亚 | 一级黄a | 在线a毛片免费视频观看 |