代碼如下:
10年積累的成都做網(wǎng)站、網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有阜寧免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
public class TimeCountDown {
public static void countDown(int seconds) {
System.err.println("倒計時" + seconds + "秒,倒計時開始:");
int i = seconds;
while (i 0) {
System.err.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i--;
}
System.err.println(i);
System.err.println("倒計時結(jié)束");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
countDown(60);// 倒計時1分鐘
}
}
public?class?Timer?implements?Runnable?{
int?i=10;
public?static?void?main(String[]?args)?{
Timer?t=new?Timer();
Thread?th=new?Thread(t);
th.start();
}
@Override
public?void?run()?{
try?{
while(true){
i--;
Thread.sleep(1000);
System.out.println(i);
if(i==7){
System.out.println("還剩7秒爆炸");
}
if(i==1){
System.out.println("差點(diǎn)就掛了");
return;
}
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
應(yīng)該是這樣吧
主要注意一下幾點(diǎn),就可以了:
(1)用date方法進(jìn)行時間計算,Date(year,month -1 , day)構(gòu)造指定日期。
(2)new Date()獲得現(xiàn)在的時間。
(3)用date計算兩個毫秒差異,然后計算日、時、分、秒。
(4)用setInterval方法延遲啟動一個js方法,沒秒執(zhí)行一次更新。
代碼如下:
!DOCTYPE?html
html
head
meta?charset="gb2312"/meta
title倒計時/title
script
function?timer(){
var?now?=?new?Date();?
var?endDate?=?new?Date("2015",?"03",?"01");?
var?leftTime?=?endDate.getTime()-?now.getTime();?
var?leftsecond?=?parseInt(leftTime/1000);?
//var?day1=parseInt(leftsecond/(24*60*60*6));?
var?day1=Math.floor(leftsecond/(60*60*24));?
var?hour=Math.floor((leftsecond-day1*24*60*60)/3600);?
var?minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60);?
var?second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);?
document.getElementById("timer").innerHTML?=?day1?+?"天"?+?hour?+?"時"?+?minute?+?"分"?+?second?+?"秒";
//每一秒執(zhí)行一次?timer方法
setInterval("timer()",1000);
}
/script
/head
body?onload?=?"timer()"
距離2015-4-1日還有:
div?id="timer"/div
/body
/html
抱歉,之前沒看到第二個條件,重新寫了下。
在本機(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;
// 記錄所要啟動的程序
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("請輸入倒計時時間");
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注冊監(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注冊監(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ù)字
public void txtInput_KeyTyped(KeyEvent ke){
if(ke.getKeyChar() '0' || ke.getKeyChar() '9'){
ke.setKeyChar('\0');
}
}
// 點(diǎn)擊按鈕時的事件處理,核心!
public void btnEnter_ActionPerformed(ActionEvent ae){
// 獲得用戶輸入的倒計時時間
String strTime = this.txtInput.getText();
if(strTime.equals("")){
// 檢測用戶是否輸入
this.lblTime.setText("您尚未輸入,請輸入!");
}
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(){
// 啟動其他程序
public void run() {
try {
// 當(dāng)程序不存在時,會進(jìn)行創(chuàng)建;存在時直接調(diào)用。
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;
// 構(gòu)造方法傳入,顯示事件的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();
}
}
}
}
基于控制臺的話很簡單的,我跟你說一下大體思路吧,二話不說先來個for循環(huán),然后輸出倒計時的數(shù)字,程序睡一秒,在輸出倒計時數(shù)字,如此循環(huá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)頁的還是基于用戶界面都可以使用這個思路的
import java.awt.Color;
import java.util.*;
import java.awt.*;
import java.applet.*;
public class Clock extends Applet implements Runnable
{
Thread timer=null;
Label label;
int lastxs=50,lastys=30,lastxm=50,lastym=30,lastxh=50,lastyh=30;
public void init()
{
label=new Label(" ");
setBackground(Color.white);
add(label);
}
public void paint(Graphics g)
{
int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter;
Date rightnow=new Date();
String today=rightnow.toLocaleString();
label.setText(today);
s=rightnow.getSeconds();
m=rightnow.getMinutes();
h=rightnow.getHours();
xcenter=100;
ycenter=80;
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*45+xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*45+ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*45+xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*45+ycenter);
xh=(int)(Math.cos((h*30+m*2)*3.14f/180-3.14f/2)*30+xcenter);
yh=(int)(Math.sin((h*30+m*2)*3.14f/180-3.14f/2)*30+ycenter);
g.setFont(new Font("TimesToman",Font.PLAIN,14));
g.setColor(Color.orange);
g.fill3DRect(xcenter-50,ycenter-50,100,100,true);
g.setColor(Color.darkGray);
g.drawString("12",xcenter-5,ycenter-37);
g.drawString("3",xcenter+40,ycenter+3);
g.drawString("6",xcenter-3,ycenter+45);
g.drawString("9",xcenter-45,ycenter+3);
g.setColor(Color.orange);
if(xs!=lastxs||ys!=lastys)
{
g.drawLine(xcenter,ycenter,lastxs,lastys);
}
if(xm!=lastxm||ym!=lastym)
{
g.drawLine(xcenter,ycenter-1,lastxm,lastym);
g.drawLine(xcenter-1,ycenter,lastxm,lastym);
}
if(xh!=lastxh||yh!=lastyh)
{
g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
g.drawLine(xcenter-1,ycenter,lastxh,lastyh);
}
g.setColor(Color.red);
g.drawLine(xcenter,ycenter,xs,ys);
g.drawLine(xcenter,ycenter-1,xm,ym);
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh);
g.drawLine(xcenter-1,ycenter,xh,yh);
lastxs=xs;
lastys=ys;
lastxm=xm;
lastym=ym;
lastxh=xh;
lastyh=yh;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);
timer.start();
}
}
public void stop()
{
timer=null;
}
public void run()
{
while(timer!=null)
{
try
{
Thread.sleep(1000);
}catch(InterruptedException ie){}
repaint();
}
timer=null;
}
public void update(Graphics g)
{
paint(g);
}
}
分享標(biāo)題:java倒計時代碼天貓 時間倒計時代碼
文章鏈接:http://muchs.cn/article18/docopdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、虛擬主機(jī)、面包屑導(dǎo)航、動態(tài)網(wǎng)站、全網(wǎng)營銷推廣、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)