|
一、进度条类 Gauge控件就是一个进度条,通过图形的方式表达一个程序运行的进度或其它操作的运行过程,Gauge的构造函数如下: Gauge(String label,boolean interactive,int maxValue,int initialValue) 其中,参数"label"指定了控件的标题,参数"interactive"指定了控件的类型,如果设置为true,则表示为交互类型(用于音量控制等),如设置为false,则为不交互类型,只能通过程序控制进度条。参数"maxValue"指定了进度条的最大值,参数initialValue指定了进度条的初始值。import javax.microedition.lcdui.*; public class GaugeThread extends Gauge implements Runnable { private int maxValue = 10; private int add = 1; private boolean done = false; public GaugeThread(String label, int maxValue, int initialValue) { super(label, false, maxValue, initialValue); this.maxValue = maxValue; new Thread(this).start(); } public void run() { while (!done) { int newValue = getValue() + add; if (newValue == maxValue) { add = -1; } else if (newValue == 0) { add = 1; } setValue(newValue); try { Thread.sleep(1000); } catch (InterruptedException err) { } } } void setDone() { done = true; } }
二、主程序 import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class gaugeDemo_2 extends MIDlet implements CommandListener { private Command exitCommand; private Form mainform; private GaugeThread gthread; public gaugeDemo_2() { exitCommand = new Command("Exit", Command.EXIT, 1); mainform = new Form("gauge实例"); mainform.addCommand(exitCommand); mainform.setCommandListener(this); gthread = new GaugeThread("后台自动增加",40,0); //new Thread(gthread).start(); mainform.append(gthread); mainform.get(0).setLayout(Item.LAYOUT_EXPAND|Item.LAYOUT_NEWLINE_AFTER); } protected void startApp() throws MIDletStateChangeException { Display.getDisplay(this).setCurrent(mainform); } protected void pauseApp() { } protected void destroyApp(boolean p1) { gthread.setDone(); } public void commandAction(Command c, Displayable d) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } }
|