?? timecount.java
字號:
/*
* Created on 2006-3-21
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author 軟件一班 李果 20031610108
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//import java.util.Timer;
import javax.swing.*;
import java.text.NumberFormat;
import java.net.*;
import javax.swing.Timer;
public class TimeCount extends JApplet implements Runnable{
AudioClip sound ;//播放的聲音
AudioClip sound_enemys;//播放發現敵人的聲音
AudioClip sound_letsgo;//播放開始行動的聲音
AudioClip sound_di;//播放計時的聲音
NumberFormat format;//格式化的時間顯示
Thread timer;//每秒更新一次時間
JLabel label;//創建一個JLabel來顯示時間
long remaining;//倒數還剩下多少毫秒
long lastUpdate;//計數最后一次更新的時間
boolean running;//用于中止線程的標志
public void init(){
//倒數時間6000毫秒,即六秒鐘
remaining = 6000;
//初始化JLabel來顯示時間
label = new JLabel();
//讓標簽顯示出背景色
label.setOpaque(true);
//向Applet中添加這個標簽
getContentPane().add(label,BorderLayout.CENTER);
//得到NumberFormat對象,將分鐘數和秒數轉換為串
format = NumberFormat.getNumberInstance();
//String soundURL = "D:\Program Files\Eclipse\eclipse\workspace\Time\hks1.wav";
//sound=getAudioClip("D:\Program Files\Eclipse\eclipse\workspace\Time\hks1.wav");
//聲音路徑
String soundURL = "hks1.wav";
String soundURL_enemys = "ct_enemys.wav";
String soundURL_letsgo = "letsgo.wav";
String soundURL_di = "buttonclickrelease.wav";
if(soundURL != null)
sound = getAudioClip(getDocumentBase(),soundURL);
if(soundURL_enemys != null)
sound_enemys = getAudioClip(getDocumentBase(),soundURL_enemys);
if(soundURL_letsgo != null)
sound_letsgo = getAudioClip(getDocumentBase(),soundURL_letsgo);
if(soundURL_di != null)
sound_di = getAudioClip(getDocumentBase(),soundURL_di);
}
public void start(){
running = true;//設置標志
if(timer == null){
timer = new Thread(this);//如果沒有線程,則創建一個
timer.start();//并啟動這個線程
}
}
public void run(){
if(sound_enemys != null)
sound_enemys.play();
try{Thread.sleep(2000);}
catch(InterruptedException e){}
if(sound_letsgo != null)
sound_letsgo.play();
while(running){
//狀態欄提示當前狀態
showStatus("正在倒計時……");
if(sound_di != null)
sound_di.play();
remaining -= 1000;//減去一秒鐘
//等待1000毫秒后,繼續運行
try{Thread.sleep(1000);}
catch(InterruptedException e){}
// 防止時間誤設為負值
if(remaining < 0)
remaining = 0;
int minutes = (int)(remaining/60000);
int seconds = (int)((remaining%60000)/1000);
label.setText(format.format(minutes)+":"+format.format(seconds));
//完成倒計時后,響鈴提示
if(remaining == 0){
//狀態欄提示程序當前狀態
showStatus("時間到,程序運行結束!");
//如果有聲音片斷,現在播放
if(sound != null)
sound.play();
label.setText("時間到!");
running = false;
}
}
//得到系統當前時間,單位是毫秒
//long now = System.currentTimeMillis();
}
public void stop(){
running = false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -