对用户的错误进行预警的第二种写法

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
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 AlertDemo2 extends MIDlet implements CommandListener {
private Display display;

private static final Command CMD_EXIT = new Command("Exit",Command.EXIT,1);
private static final Command CMD_BACK = new Command("Back",Command.BACK,1);
private static final Command CMD_SHOW = new Command("Show",Command.SCREEN,1);

private Form mainForm;

private ChoiceGroup types;
private ChoiceGroup timeouts;
private ChoiceGroup options;

private final static int SECOND = 1000;
private static final String[] typeStrings = {
"Alarm","Confirmation","Error","Info","Warning"
};
private AlertType[] alertTypes = {
AlertType.ALARM,AlertType.CONFIRMATION,AlertType.ERROR,AlertType.INFO,AlertType.WARNING
};
private final static String[] timeoutStrings = {
"2 Seconds","4 Seconds","8 Seconds","Forever"
};
private int[] tos = {2*SECOND,4*SECOND,8*SECOND,Alert.FOREVER};

private boolean firstTime;

public AlertDemo2(){
display = Display.getDisplay(this);

firstTime = true;
}

protected void destroyApp(boolean arg0) {
}

protected void pauseApp() {
}

protected void startApp() {
if(firstTime){
mainForm = new Form("Alert Options");

types = new ChoiceGroup("Type",Choice.POPUP,typeStrings,null);
mainForm.append(types);

timeouts = new ChoiceGroup("Timeout",Choice.POPUP,timeoutStrings,null);
mainForm.append(timeouts);

String[] optionArray = {"Show Indicator"};
options = new ChoiceGroup("Options",Choice.MULTIPLE,optionArray,null);
mainForm.append(options);

mainForm.addCommand(CMD_SHOW);
mainForm.addCommand(CMD_EXIT);
mainForm.setCommandListener(this);
}
display.setCurrent(mainForm);
firstTime = false;
}

public void commandAction(Command c, Displayable d) {
if(c == CMD_SHOW){
Alert alert = new Alert("Alert");

int typeIndex = types.getSelectedIndex();
alert.setType(alertTypes[typeIndex]);

int timeoutIndex = timeouts.getSelectedIndex();
alert.setTimeout(tos[timeoutIndex]);

boolean[] optionIndex = new boolean[1];
options.getSelectedFlags(optionIndex);

if(optionIndex[0]){
Gauge gauge = createGauge(tos[timeoutIndex]);
alert.setIndicator(gauge);
}
alert.addCommand(CMD_BACK);

display.setCurrent(alert);
}else if(c == CMD_EXIT){
destroyApp(false);
notifyDestroyed();
}
}

private Gauge createGauge(int maxValue){
if(maxValue == Alert.FOREVER){
return new Gauge(null,false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING);
}

final int max = maxValue / SECOND;

final Gauge gauge = new Gauge(null,false,max,0);

new Thread(){
public void run(){
int value = 0;

while(value < max){
gauge.setValue(value);
++value;

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();

return gauge;
}

}

你可能感兴趣的:(thread,C++,c,C#)