大家帮我看看这个java文字移动的程序~~

[复制链接]
查看11 | 回复1 | 2010-11-3 12:21:24 | 显示全部楼层 |阅读模式
问题:每次文字移动的时候怎么不会擦除之前画出来的文字呢?我已经重写update()方法了,把背景填充了白色,怎么不行呢?而且调试了下好像update方法的内容根本不会被调用,是repaint()方法放的位置错了吗?请高手帮我看看,万分感谢!!
代码:
import java.awt.*;
import javax.swing.*;
public class MovementWord extends JFrame implements Runnable{

private int xPos = 50;

private int yPos = 50;

private String name = "hr_vincent";



MovementWord() {

super("MovementWord");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(200, 100, 800, 600);

}



public static void main(String[] args) {

MovementWord mw = new MovementWord();

mw.setVisible(true);

new Thread(mw).start();



}



public void paint(Graphics g) {

Color c = g.getColor();

g.setColor(Color.BLACK);

g.drawString(this.name, xPos, yPos);

g.setColor(c);

}



public void run() {

while(true) {

try {

Thread.sleep(300);

} catch(InterruptedException e) {

}

xPos += 10;

repaint();

}

}



public void update(Graphics g) {

g.setColor(Color.WHITE);

g.fillRect(0, 0, 800,600);

}
}

回复

使用道具 举报

千问 | 2010-11-3 12:21:24 | 显示全部楼层
paint开始添加一行
update(g);
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行