`
zj360202
  • 浏览: 30125 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

用来提示用户错误和异常的类 AlertDemo.java

    博客分类:
  • j2me
阅读更多
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
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 AlertDemo extends MIDlet {
private Display display;

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

private Form mainForm;

private final static int SECOND = 1000;
private static final String[] typeStrings = {
"Alarm","Confirmation","Error","Info","Warning"
};
private final static String[] timeoutStrings = {
"2 Seconds","4 Seconds","8 Seconds","Forever"
};

private boolean firstTime;

public AlertDemo(){
firstTime = true;
mainForm = new Form("Alert Options");
}

protected void startApp() {
display = Display.getDisplay(this);

showOption();
}

private void showOption(){
if(firstTime){
ChoiceGroup types = new ChoiceGroup("Type",ChoiceGroup.POPUP,typeStrings,null);
mainForm.append(types);

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

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

mainForm.addCommand(CMD_SHOW);
mainForm.addCommand(CMD_EXIT);
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;
ChoiceGroup timeoutsCG;
ChoiceGroup indicatorCG;

int[] timeouts = {2*SECOND,4*SECOND,8*SECOND,Alert.FOREVER};

public AlerListener(ChoiceGroup types,ChoiceGroup timouts,ChoiceGroup indicator){
typesCG = types;
timeoutsCG = timouts;
indicatorCG = indicator;
}

public void commandAction(Command c, Displayable d) {
if(c == CMD_SHOW){

Alert alert = new Alert("Alert");

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

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

alert.setString(typeStrings[typeIndex] + " Alert, Running "
+ timeoutStrings[timeoutIndex]);

boolean[] selectFlags = new boolean[1];
indicatorCG.getSelectedFlags(selectFlags);

if(selectFlags[0]){
Gauge indicator = createIndicator(timeouts[timeoutIndex]);
alert.setIndicator(indicator);
}

display.setCurrent(alert);

}else if(c == CMD_EXIT){
destroyApp(false);
notifyDestroyed();
}
}

}

protected void destroyApp(boolean arg0) {
}

protected void pauseApp() {
}

public 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 (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();

return indicator;
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics