过时的API问题 在编写书上的程序时出现了这样的错误: 注意:Animation.java 使用或覆盖了已过时的 API。 注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。API过时了? 不知该如何解决。希望大家指点! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般过时的方法都有替代的方法,你从api找下。 @SuppressWarnings("unchecked")把这个放在过时的方法上一行,取消警告。 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); }}发上源代码 希望大家看看是哪个方法过时 runner.stop();不推荐使用了。建议在线程代码中while(true) ==> while (运行条件) 那该用哪个方法来替代 thread.stop 呢? 推荐的方法是,自然而然的结束run,也就是跳出那个while循环通过设置一个变量,class T extends Thread { public boolean running = true; // 偷懒了,直接public public void run() { while (running) { System.out.println("running"); } // 这里可以做一些收尾工作,而stop()是非正常死亡,没法完成类似任务 }}然后在判断需要结束的时候,把running==> false,那个线程自然就结束了。 在eclipse下的话,你用ctrl+鼠标左键点击那个过时的类名,进入那个类去看看。看它的替代类是什么。然后换着那个类来使用即可。 谢谢楼上 程序正确运行了但是有没有替代 thread.stop的方法? java中访问权限的小问题 异常中printStackTrace()中的一些异常情况 子类 myclass1= new 父类();父类 myclass2= new 子类();谁对谁错? 【高分求助】有没有过滤掉hashmap中key不同,值相同的高效算法? 如何取得树中的所有叶子节点? 有关一个字符串的问题? tomcat下的应用在考到weblogic 下报错 请教! cvs我在release的时候报告错误:up-to-date check failed:〈文件名〉 在提交表单之前的数据类型如何进行一次性检验其是否正确? 有没有这样一本书 在JTable响应鼠标右键
把这个放在过时的方法上一行,取消警告。
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);
}
}
发上源代码 希望大家看看是哪个方法过时
while(true) ==> while (运行条件)
通过设置一个变量,class T extends Thread {
public boolean running = true; // 偷懒了,直接public
public void run() {
while (running) {
System.out.println("running");
}
// 这里可以做一些收尾工作,而stop()是非正常死亡,没法完成类似任务
}
}然后在判断需要结束的时候,把running==> false,那个线程自然就结束了。