利用Applet类和Runnable接口实现滚动字幕

利用Applet类和Runnable接口实现滚动字幕,其中字幕文字(“学好java”)和时间间隔(“200”)需要由页面文件中标记的子标记传递。

package p1;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
@SuppressWarnings("serial")
public class MoveMessage extends JApplet implements Runnable {
	String str;
	int time;
	private Thread thread;
	public void init() {
		setBackground(Color.PINK);
		str = getParameter("message");
		String timeArg = getParameter("time");
		time = Integer.parseInt(timeArg);;
		thread = new Thread(this);
	}
	public void start() {
		thread.start();
	}
	public void run() {
		int x = 0;
		Graphics g = getGraphics();
		while (true) {
			try {
				Thread.sleep(time);
			} catch (Exception e) {
				e.printStackTrace();
			}
			g.clearRect(0, 0, getWidth(), getHeight());
			g.drawString(str, x, 30);
			x += 2;
			if (x >= getWidth())
				x = 0;
		}
	}
}

你可能感兴趣的:(java100题,java)