*心得:TimeZone tz1=TimeZone.getTimeZone("Europe/Paris");
* Calendar cld=Calendar.getInstance(tz);
* clk.setText(cld.get(Calendar.HOUR_OF_DAY)+":"+cld.get(Calendar.MINUTE)+":"+cld.get(Calendar.SECOND));
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class WorldClock{
Frame f=new Frame("WorldClock");
Label l1=new Label();
Label l2=new Label();
Label l3=new Label();
Label cl1=new Label();
Label cl2=new Label();
Label cl3=new Label();
public WorldClock(){
l1.setFont(new Font("Arial",Font.BOLD,30));
l2.setFont(new Font("Arial",Font.BOLD,30));
l3.setFont(new Font("Arial",Font.BOLD,30));
cl1.setFont(new Font("Arial",Font.BOLD,30));
cl2.setFont(new Font("Arial",Font.BOLD,30));
cl3.setFont(new Font("Arial",Font.BOLD,30));
cl1.setForeground(Color.red);
cl2.setForeground(Color.red);
cl3.setForeground(Color.red);
f.setLayout(new GridLayout(2,3));
f.add(l1);
f.add(l2);
f.add(l3);
f.add(cl1);
f.add(cl2);
f.add(cl3);
TimeZone tz1=TimeZone.getTimeZone("Europe/Paris");
clock c1=new clock(l1,cl1,tz1);
new Thread(c1).start();
TimeZone tz2=TimeZone.getTimeZone("Asia/Shanghai");
clock c2=new clock(l2,cl2,tz2);
new Thread(c2).start();
TimeZone tz3=TimeZone.getTimeZone("Europe/Rome");
clock c3=new clock(l3,cl3,tz3);
new Thread(c3).start();
f.setLocation(200,200);
f.setVisible(true);
f.pack();
}
public static void main(String[] args){
new WorldClock();
String[] s=TimeZone.getAvailableIDs();
int i=0;
while(++i<s.length){
System.out.println (s[i]);
}
}
}
class clock implements Runnable{
private Label l;
private Label clk;
TimeZone tz;
public clock(Label l,Label clk,TimeZone tz){
this.l=l;
this.clk=clk;
this.tz=tz;
}
public void run(){
l.setText(tz.getID());
while(true){
Calendar cld=Calendar.getInstance(tz);
clk.setText(cld.get(Calendar.HOUR_OF_DAY)+":"+cld.get(Calendar.MINUTE)+":"+cld.get(Calendar.SECOND));
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
} |