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

本文介绍了一个使用Java ME (MIDP) 编写的简单示例程序,该程序展示了如何创建不同类型的弹窗提示(Alert),包括警报、确认、错误、信息和警告,并允许用户选择不同的超时选项及是否显示指示器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值