alertdemo.java
来自「j2me学习 简单例子」· Java 代码 · 共 188 行
JAVA
188 行
package example.demoseniorui.alert;
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;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* <p>J2ME Alert Demo</p>
*
* @author 刘光辉
* @version 2009.01.23
*/
public class AlertDemo extends MIDlet {
private static final int SECOND = 1000;
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 static final String[] typeStrings = { "Alarm", "Confirmation",
"Error", "Info", "Warning" };
private static final String[] timeoutStrings = { "2 Seconds", "4 Seconds",
"8 Seconds", "Forever" };
private Display display;
private boolean firstTime;
private Form mainForm;
/**
* <p>AlertDemo的构造函数</p>
*/
public AlertDemo() {
firstTime = true;
mainForm = new Form("Alert Options");
}
/**
* <p>启动程序,使MIDlet处于active状态</p>
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
showOption();
}
/**
* <p>创建这个MIDlet的基本显示</p>
* <p>在这个方法里可以选择Alert的类型和特性</p>
*/
private void showOption() {
if (firstTime) {
ChoiceGroup types = new ChoiceGroup("Types", ChoiceGroup.POPUP,
typeStrings, null);
mainForm.append(types);
ChoiceGroup timeouts = new ChoiceGroup("Timeouts",
ChoiceGroup.POPUP, timeoutStrings, null);
mainForm.append(timeouts);
String[] optionStrings = { "Show Indicator" };
ChoiceGroup options = new ChoiceGroup("Options", ChoiceGroup.POPUP,
optionStrings, null);
mainForm.append(options);
mainForm.addCommand(CMD_SHOW);
mainForm.addCommand(CMD_EXIT);
mainForm.setCommandListener(new AlertListener(types, timeouts,
options));
firstTime = false;
}
display.setCurrent(mainForm);
}
/**
* <p>暂停程序,使MIDlet处于pause状态</p>
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp() {
}
/**
* <p>销毁程序,使MIDlet处于destroyed状态</p>
*
* @param boolean unconditional
* <ul>true 释放资源保存数据进入destroyed状态,false 抛出异常保持当前状态</ul>
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
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);
// if (maxValue != Gauge.INDEFINITE) {
new Thread() {
public void run() {
int value = 0;
while (value < max) {
indicator.setValue(value);
++value;
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
// ignore
}
}
}
}.start();
// }
return indicator;
}
class AlertListener implements CommandListener {
AlertType[] alertTypes = { AlertType.ALARM, AlertType.CONFIRMATION,
AlertType.ERROR, AlertType.INFO, AlertType.WARNING };
ChoiceGroup typesCG;
int[] timeouts = { 2 * SECOND, 4 * SECOND, 8 * SECOND, Alert.FOREVER };
ChoiceGroup timeoutsCG;
ChoiceGroup indicatorCG;
/**
* <p>AlertListener的构造函数</p>
* @param ChoiceGroup types 类型
* @param ChoiceGroup timeouts 时间间隔
* @param ChoiceGroup indicator 跳转指向
*/
public AlertListener(ChoiceGroup types, ChoiceGroup timeouts,
ChoiceGroup indicator) {
typesCG = types;
timeoutsCG = timeouts;
indicatorCG = indicator;
}
/**
* <p>命令处理函数,实现commandListener接口</p>
* @param Command c 命令对象
* @param Displayable d 被放置到显示屏显示的对象
*/
public void commandAction(Command c, Displayable d) {
if (c == CMD_SHOW) {
int typeIndex = typesCG.getSelectedIndex();
Alert alert = new Alert("Alert");
alert.setType(alertTypes[typeIndex]);
int timeoutIndex = timeoutsCG.getSelectedIndex();
alert.setTimeout(timeouts[timeoutIndex]);
alert.setString(typeStrings[typeIndex] + " Alert, Running "
+ 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 == CMD_EXIT) {
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
notifyDestroyed();
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?