⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dazi.java

📁 这是一个关于模拟器上显示的打字机效果的代码
💻 JAVA
字号:
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.midp.lcdui.Text;

public class Dazi extends MIDlet implements Runnable {
	//声明部分
	Form form;
	Command cmdStart, cmdStop;
	String str;
	boolean Running;
	Thread thread;
	StringBuffer buffer;
	int index = 0;
    //构造方法
	public Dazi() {
		super();
		form=new Form("打字机效果");
		str="夜来风雨声,花落知多少。";		
		buffer=new StringBuffer();
		cmdStart=new Command("开始",Command.OK,0);
		cmdStop=new Command("停止",Command.EXIT,1);
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		Stop();
	}

	protected void pauseApp() {
		Stop();
	}
     //添加组件
	protected void startApp() throws MIDletStateChangeException {
		Display.getDisplay(this).setCurrent(form);
		form.addCommand(cmdStart);
	    form.addCommand(cmdStop);
	    // 注册监听器,编写事件处理代码
		form.setCommandListener(new CommandListener(){		    	
			public void commandAction(Command c, Displayable d) {
				if(c==cmdStart){
	    			Start();
	    	}else{				
	    			Stop();
	    	}
		}
		});
	 }

	public void Start() {
		Running =true;
		thread = new Thread(this);
		thread.start();
	}

	public void Stop() {
		Running = false;
	}

	public void run() {
		
		while(Running==true){
		 form.deleteAll();
		 buffer.append(str.charAt(index));
		 form.append(buffer.toString());
		 if(index<str.length()-1)
				index++;
		 try {
			Thread.sleep(300);
		} catch (InterruptedException e) {}
	}
		
				index=0;
				buffer.delete(0, str.length());
			}
			
		}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -