本帖最后由 HZAU_ZCY 于 2011-06-04 14:32:24 编辑

解决方案 »

  1.   

    不知道是{}匹配的问题还是什么?帮我找下BUG 谢谢
      

  2.   

    参数错误:
    addWindowLisenter(new WindowAdapter()
    接口定义
    interface  TimerListener{
        void timeElapsed(Timer t);
    }
    删掉}?,以及其他的多余的垃圾?
    }?
       public void run()还有什么问题?你要描述清楚
      

  3.   

    两处错误:
    1.addWindowListener那句,你的Listener拼错了
    2.public void run,你写在了Timer的外部
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;public class TimerTest {    public static void main(String args[]) {
            JFrame f = new TimerTestFrame();
            f.setVisible(true);
        }
    }class TimerTestFrame extends JFrame {    public TimerTestFrame() {
            setSize(300, 450);
            setTitle("Timeer Test");
            addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            Container c = getContentPane();
            c.setLayout(new GridLayout(2, 3));
            c.add(new ClockCanvas("San Jose", "GMT-8"));
            c.add(new ClockCanvas("Taipei", "GMT+8"));
            c.add(new ClockCanvas("Berlin", "GMT+1"));
            c.add(new ClockCanvas("New York", "GMT-5"));
            c.add(new ClockCanvas("Cairo", "GMT+2"));
            c.add(new ClockCanvas("Bombay", "GMT+5"));
        }
    }interface TimerListener {    void timeElapsed(Timer t);    class Timer extends Thread {        private TimerListener target;
            public int interval;        public Timer(int i, TimerListener t) {
                target = t;
                interval = i;
                setDaemon(true);
            }
            public void run(){
                try {
                    while (!interrupted()) {
                        sleep(interval);
                        target.timeElapsed(this);
                    }
                } catch (InterruptedException e) {
            }
        }
        }}class ClockCanvas extends JPanel implements TimerListener {    private int seconds = 0;
        private String city;
        private int offset;
        private GregorianCalendar calendar;
        private final int LOCAL = 16;    public ClockCanvas(String c, String tz) {
            city = c;
            calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
            Timer t = new Timer(1000, this);
            t.start();
            setSize(125, 125);
        }    public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(0, 0, 100, 100);
            double hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60) / (12 * 60 * 60);
            double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60);
            double secondAngle = 2 * Math.PI * (seconds - 15) / 60;
            g.drawLine(50, 50, 50 + (int) (30 * Math.cos(hourAngle)),
                    50 + (int) (30 * Math.sin(hourAngle)));
            g.drawLine(50, 50, 50 + (int) (40 * Math.cos(minuteAngle)),
                    50 + (int) (40 * Math.sin(minuteAngle)));
            g.drawLine(50, 50, 50 + (int) (45 * Math.cos(secondAngle)),
                    50 + (int) (45 * Math.sin(secondAngle)));
            g.drawString(city, 0, 115);
        }    public void timeElapsed(Timer t) {
            calendar.setTime(new Date());
            seconds = calendar.get(Calendar.HOUR) * 60 * 60 + calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND);
            repaint();
        }
    }