谁帮我看看为什么我想显示的时间标签在applet中没有反应??效果应该是从右到左循环移动的,但是我的却没反应??请高手赐教,回答正确40分全给了。import java.awt.*;
import java.applet.Applet;
import java.util.Calendar;
import java.util.GregorianCalendar;class Clock8  extends Label implements Runnable{ Thread thread;
public Clock8(){
if(thread==null){
thread=new Thread(this);
thread.start();
}
}


    @Override
public void run() {

                String s = "";
        while (true) {
            Calendar c = new GregorianCalendar();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);
            int second = c.get(Calendar.SECOND);
            String ss = "";
            String mm = "";
            String hh = "";
            ss = second < 10 ? "0" + String.valueOf(second) : String.valueOf(second);
            mm = minute < 10 ? "0" + String.valueOf(minute) : String.valueOf(minute);
            hh = hour < 10 ? "0" + String.valueOf(hour) : String.valueOf(hour);
            s = hh + ":" + mm + ":" + ss;
            this.setText(s);
            this.setFont(new Font("",Font.BOLD,60));
            this.setForeground(Color.YELLOW);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
        } }
  }
public class RollWords extends Applet implements Runnable {
Clock8 c1=new Clock8();
private String rollWords;
private int size;
private int color;
int x, y;
Thread rollThread = null;
 public void init() { 
 rollWords= c1.getText();
 
 
//  rollWords =cl.getText() ;
  // size = Integer.parseInt(getParameter("size"));
  // color = Integer.parseInt(getParameter("color"));
  size = 18;
  color = 1800;
  x = 390;
  y = 40;
 }

    @Override
 public void paint(Graphics g) {
  Color c = new Color(color);
  g.setColor(c);
  Font f = new Font("", 1, size);
  g.setFont(f);
  g.drawString(rollWords, 300 - x, y);
 }

    @Override
 public void start(){
  if(null == rollThread){
   rollThread = new Thread(this);
   rollThread.start();
  }
 }
 public void run() {
  while (true) {
   repaint();
   try {
    Thread.sleep(100);
   } catch (InterruptedException ex) {

   }
   x = (x+5)%400;
  }
 }
}

解决方案 »

  1.   

    跑了一下.。没改过.没问题呀.的确实现了时间从左往右移动.。不过跑几次发现时间不能刷新.。d(>_<)b
      

  2.   


    public class Clock8 extends Label implements Runnable { Thread thread; public Clock8() {
    if (thread == null) {
    thread = new Thread(this);
    thread.start();
    }
    } @Override
    public void run() { String s = "";
    while (true) {
    Calendar c = new GregorianCalendar();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    int second = c.get(Calendar.SECOND);
    String ss = "";
    String mm = "";
    String hh = "";
    ss = second < 10 ? "0" + String.valueOf(second) : String
    .valueOf(second);
    mm = minute < 10 ? "0" + String.valueOf(minute) : String
    .valueOf(minute);
    hh = hour < 10 ? "0" + String.valueOf(hour) : String.valueOf(hour);
    s = hh + ":" + mm + ":" + ss;
    this.setText(s);
    this.setFont(new Font("", Font.BOLD, 60));
    this.setForeground(Color.YELLOW);

    //.....
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    }
    }
    }
    public class RollWords extends Applet implements Runnable { Clock8 c1 = new Clock8();
    private int size;
    private int color;
    int x, y;
    Thread rollThread = null; public RollWords() {
    new Thread(c1).start();
    } public void init() {
    // size = Integer.parseInt(getParameter("size"));
    // color = Integer.parseInt(getParameter("color"));
    size = 18;
    color = 1800;
    x = 390;
    y = 40;
    } @Override
    public void paint(Graphics g) {
    Color c = new Color(color);
    g.setColor(c);
    Font f = new Font("", 1, size);
    g.setFont(f);
    g.drawString(c1.getText(), 300 - x, y);
    System.out.println("-------------------");
    } @Override
    public void start() {
    if (null == rollThread) {
    rollThread = new Thread(this);
    rollThread.start();
    }
    } public void run() {
    while (true) {
    repaint();
    try {
    Thread.sleep(100);
    } catch (InterruptedException ex) { }
    x = (x + 5) % 400;
    }
    }
    }
    楼主运行一下就明白了。
    话说楼主帖上来的代码,也真让我无语。不过楼主可要记得给分噢。
      

  3.   

    能告诉我原因为什么我的rollWords= c1.getText();句话放在init里不行啊??能解释下吗??
      

  4.   


    new Thread(c1).start();
    直接的原因是这里。你线程根本就没有启动,也没有调用setText()方法。
    你不断的更新text的值,你放在init方法里面只会取一次。第二次则不会去取。