在编写书上的程序时出现了这样的错误:    注意:Animation.java 使用或覆盖了已过时的 API。
    注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。API过时了? 不知该如何解决。希望大家指点!

解决方案 »

  1.   

    一般过时的方法都有替代的方法,你从api找下。
      

  2.   

    @SuppressWarnings("unchecked")
    把这个放在过时的方法上一行,取消警告。
      

  3.   

    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Color;public class Animation extends java.applet.Applet implements Runnable {
    Image img;
    Thread runner;
    int xpos = 0;
    int ypos = 50;

    public void init() {
    img = getImage(getCodeBase(),"ring.jpg");
    }

    public void start() {
    if(runner == null) {
    runner = new Thread(this);
    runner.start();
    }
    }

    public void stop() {
    if(runner != null) {
    runner.stop();
    runner = null;
    }
    }

    public void run() {
    //initialize
    while(true) {
    for(xpos = 0; xpos < getSize().width; xpos += 5) {
    repaint();
    try { runner.sleep(100);}
    catch(InterruptedException e) {}
    }
    }
    }

    public void paint(Graphics g) {
    g.drawImage( img, xpos + 10, ypos, this);
    }
    }
    发上源代码 希望大家看看是哪个方法过时
      

  4.   

    runner.stop();不推荐使用了。建议在线程代码中
    while(true) ==> while (运行条件)
      

  5.   

    那该用哪个方法来替代 thread.stop 呢?
      

  6.   

    推荐的方法是,自然而然的结束run,也就是跳出那个while循环
    通过设置一个变量,class T extends Thread {
      public boolean running = true; // 偷懒了,直接public
      public void run() {
        while (running) {
          System.out.println("running");
        }
        // 这里可以做一些收尾工作,而stop()是非正常死亡,没法完成类似任务
      }
    }然后在判断需要结束的时候,把running==> false,那个线程自然就结束了。
      

  7.   

    在eclipse下的话,你用ctrl+鼠标左键点击那个过时的类名,进入那个类去看看。看它的替代类是什么。然后换着那个类来使用即可。
      

  8.   

    谢谢楼上  程序正确运行了但是有没有替代 thread.stop的方法?