J2ME Gauge 组件测试

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


//Gauge组件测试
public class Gauge_test extends MIDlet implements CommandListener {
	
	private Display display;
	private Alert al;
	
	public Gauge_test(){
		display = Display.getDisplay(this);
	}
	
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		al = new Alert("信息发送中");
		al.setType(AlertType.INFO);
		al.setTimeout(Alert.FOREVER);
		Gauge g = new Gauge(null,false,10,0);
		al.setIndicator(g);
		Command start = new Command("开始",Command.OK,1);
		Command stop = new Command("停止",Command.STOP,1);
		al.addCommand(start);
		al.addCommand(stop);
		al.setCommandListener(this);
		display.setCurrent(al);
		
	}

	public void commandAction(Command c, Displayable d) {
		// TODO Auto-generated method stub
		String cmd = c.getLabel();
		if(cmd.equals("开始")){
			AlertThread t = new AlertThread(al);
			al.setString("正在发送信息...");
			t.start();
		}else if(cmd.equals("停止")){
			notifyDestroyed();
		}
	}

}
//编写一个类
class AlertThread extends Thread{
	Alert al;
	public AlertThread(Alert al){
		this.al = al;
	}
	public void run(){
		Gauge indicator = al.getIndicator();
		for(int i= 0;i<11;i++){
			indicator.setValue(i);
			try{
				Thread.sleep(500);
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}
}

猜你喜欢

转载自sunzone.iteye.com/blog/1846115