?? chap10-4.txt
字號:
// 程序10-4
import java.awt.*;
import java.applet.*;
import java.util.Date; // 獲取當前時間
import java.text.DateFormat; // 將時間轉換為一個字符串
public class clock extends Applet implements Runnable{
DateFormat timeFormat;
Thread timer; // 更新時間的線程
boolean running; // 停止線程的運行
public void init( ){
timeFormat=DateFormat.getDateTimeInstance( ); // 日期時間格式
}
public void start( ){
running=true;
if(timer==null){ // 如果還沒有啟動線程,創建一個時間線程
timer=new Thread(this); // 創建線程
timer.start( ); // 啟動線程
}
}
public void run( ){ // 覆蓋了接口Runnable中的run方法
while(running){
showStatus(timeFormat.format(new Date( ))); // 獲取最新日期時間
try{
Thread.sleep(1000); // 睡眠1秒
}catch(InterruptedException e){
System.exit(0);
}
timer=null; // 如果線程退出,將其變成垃圾,這樣再次調用start方法時,
// 可以重新創建新線程
}
}
public void stop( ){
running=false;
timer.stop( ); // 終止線程
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -