模拟试题

3773考试网计算机等级考试模拟试题正文

2011计算机二级JAVA编程:多线程的世界时钟,显示巴黎,罗马,上海时间, AWT界面

来源:fjzsksw.com 2010-11-5 8:47:00

 

 

  多线程的世界时钟,显示巴黎,罗马,上海时间, AWT界面

  /*心得: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

  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();

  }

  }

  }

  }

 

 

触屏版 电脑版
3773考试网 琼ICP备12003406号-1