package com.brj.test;
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* 重点练习Alert类
* @author brj
*
*/
public class TestAlert extends MIDlet implements CommandListener {
private Display display;
// the back face
private TextBox mainFace;
// the alert face
private Alert alert;
// exit Command button
private Command exitCommand;
// alert Command button
private Command alertCommand;
public TestAlert() {
// get the instance of the Display
display = Display.getDisplay(this);
String mainFaceInstruction = "This is the place providing space to write!";
mainFace = new TextBox("welcome", mainFaceInstruction, 100,
TextField.ANY);
// the Image of the alert
Image alertImage = null;
try {
alertImage = Image.createImage("/gril.png");
} catch (IOException e) {
e.printStackTrace();
}
alert = new Alert("AlertTitle", " Sorry Error!",
alertImage, AlertType.INFO);
// set the timeout
alert.setTimeout(Alert.FOREVER);
// Command
exitCommand = new Command("Exit", Command.EXIT, 1);
alertCommand = new Command("Alert", Command.SCREEN, 0);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
mainFace.addCommand(exitCommand);
mainFace.addCommand(alertCommand);
mainFace.setCommandListener(this);
// set the Current Display
display.setCurrent(mainFace);
}
public void commandAction(Command c, Displayable d) {
// exit Command monitor
if (c == exitCommand) {
try {
this.destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
this.notifyDestroyed();
}
// alert interphase
if (c == alertCommand && d == mainFace) {
display.setCurrent(alert, mainFace);
}
}
}