发新话题
移动帖子 加入精华 加入置顶 加入收藏 关注此帖

AWT用多线程显示各个地区的时间





AWT用多线程显示各个地区的时间

运行效果:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class T {
  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 T() {
    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 T();
    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();
      }
    }
  }
}

2008-07-31_065223.gif
快乐渡过每一天,减肥坚持每一天



编辑 回复 快速回复 TOP


Re:AWT用多线程显示各个地区的时间

学习一下!
编辑 回复 快速回复 TOP
发新话题