-- 作者:admin
-- 发布时间:2008/9/25 10:11:12
-- 程序代码——结合Alert的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.Form; import javax.microedition.lcdui.Gauge; import javax.microedition.midlet.MIDlet;
public class Exec extends MIDlet implements CommandListener { Display display; Gauge gauge = new Gauge("利用多线程演示Gauge的使用方法", false, 10, 0); boolean issafeexit = true; Form form = new Form("表单"); Command cmd1 = new Command("开始", Command.OK, 1); Command cmd2 = new Command("退出", Command.BACK, 1); Thread thread;
public Exec() { display = Display.getDisplay(this); }
public void startApp() { form.append(gauge); form.addCommand(cmd1); form.addCommand(cmd2); form.setCommandListener(this); display.setCurrent(form); }
public void pauseApp() { display.setCurrent(null); }
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable arg1) { if (c == cmd1) { gauge.setLabel("正在进行处理,可以单击开始按钮重新开始"); gauge.setValue(0); form.removeCommand(cmd1); thread = new Thread(new GaugeUpdater()); thread.start(); } else if (c == cmd2) { if (issafeexit == true) { destroyApp(false); notifyDestroyed(); } else { Alert alert = new Alert("系统正忙", "请等会再试", null, AlertType.WARNING); alert.setTimeout(500); display.setCurrent(alert); } } }
// 新增一个线程处理进度条,定义一个内部类 class GaugeUpdater implements Runnable { GaugeUpdater() { }
public void run() { issafeexit = false; try { while (gauge.getValue() < gauge.getMaxValue()) { Thread.sleep(500); gauge.setValue(gauge.getValue() + 1); } issafeexit = true; gauge.setLabel("处理完毕可以安全退出了"); form.addCommand(cmd1); } catch (Exception e) { System.out.println(e.getMessage()); } } } }
[此贴子已经被作者于2010-12-12 18:32:08编辑过]
|