import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDlet; //made by sword2008@ alert使用 public class AlertDemo extends MIDlet { //private private Command showcmd=new Command("SHOW",Command.SCREEN,1); private Command exitcmd=new Command("EXIT",Command.EXIT,1); private String[] typeStrings={"alarm","confirmation","error","info","warning"}; private String[] timeoutStrings={"2秒","4秒","8秒","永远"}; private int SECOND=1000; private Display display; private boolean firstTime; private Form mainForm; public AlertDemo(){firstTime=true;mainForm=new Form("警报设置");} public void startApp(){display=Display.getDisplay(this);showOption();} private void showOption(){ if(firstTime){ ChoiceGroup types=new ChoiceGroup("Type",ChoiceGroup.POPUP,typeStrings,null); ChoiceGroup timeouts=new ChoiceGroup("Timeout",ChoiceGroup.POPUP,timeoutStrings,null); String[] optionStrings={"show Indicator"}; ChoiceGroup options=new ChoiceGroup("OPTIONS",ChoiceGroup.MULTIPLE,optionStrings,null); mainForm.append(types); mainForm.append(timeouts); mainForm.append(options); mainForm.addCommand(exitcmd); mainForm.addCommand(showcmd); mainForm.setCommandListener(new AlerListener(types,timeouts,options));firstTime=false; } display.setCurrent(mainForm); } private class AlerListener implements CommandListener{ AlertType[] alertTypes={AlertType.ALARM,AlertType.CONFIRMATION,AlertType.ERROR,AlertType.INFO,AlertType.WARNING}; ChoiceGroup typesCG,timeoutsCG,indicatorCG; int[] timeouts={2*SECOND,4*SECOND,8*SECOND,Alert.FOREVER}; public AlerListener(ChoiceGroup types,ChoiceGroup timeouts, ChoiceGroup indicator){ typesCG=types; timeoutsCG=timeouts; indicatorCG=indicator; }public void commandAction(Command c,Displayable d){if(c==showcmd){int typeIndex=typesCG.getSelectedIndex();Alert alert=new Alert("警报");alert.setType(alertTypes[typeIndex]);int timeoutIndex=timeoutsCG.getSelectedIndex();alert.setTimeout(timeouts[timeoutIndex]);alert.setString(typeStrings[typeIndex]+"警报正在运行"+timeoutStrings[timeoutIndex]); boolean [] SelectedFlags=new boolean[1]; indicatorCG.getSelectedFlags(SelectedFlags); if(SelectedFlags[0]){ Gauge indicator=createIndicator(timeouts[timeoutIndex]); alert.setIndicator(indicator);} display.setCurrent(alert);}else if(c==exitcmd){ destroyApp(false); notifyDestroyed();}} } public void destroyApp(boolean unkowmn){} public void pauseApp(){} private Gauge createIndicator(int maxValue){ if(maxValue==Alert.FOREVER){ return new Gauge(null,false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING); final int max=maxValue/SECOND; final Gauge indicator=new Gauge(null,false,max,0); new Thread(){ public void run(){ int value=0; while(value<max){ indicator.setValue(value); ++value; try{Thread.sleep(1000);} catch(Exception e){} } } }.start(); return indicator; } } }/*注意:如果出现的错误:Project settings savedBuilding "TextBoxDemo"f:\WTK22\apps\TextBoxDemo\src\TextBoxDemo.java:5: TextBoxDemo is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable) in javax.microedition.lcdui.CommandListenerpublic class TextBoxDemo extends MIDlet implements CommandListener ^1 errorcom.sun.kvem.ktools.ExecutionExceptionBuild failed 一定是commandAction出现问题,检查一下两个形式参数(Command cmd,Displayable dis)注意是Displayable而不是Display或者检查commandAction的c是否为大写 */

评论