代码如下:
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMIDlet extends MIDlet implements CommandListener{
privateboolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc="Generated Fields ">
privateCommand exitCommand;
private Formform;
privateImageItem imageItem;
privateImage image1;
//</editor-fold>
publicHelloMIDlet() {
}
//<editor-fold defaultstate="collapsed" desc="Generated Methods ">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: initialize ">
private voidinitialize() {
// write pre-initialize user code here
// write post-initialize user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: startMIDlet ">
public voidstartMIDlet() {
// write pre-action user code here
switchDisplayable(null, getForm());
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: resumeMIDlet ">
public voidresumeMIDlet() {
// write pre-action user code here
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: switchDisplayable ">
public voidswitchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: commandAction for Displayables">
public voidcommandAction(Command command, Displayable displayable) {
// write pre-action user code here
if (displayable == form) {
if (command == exitCommand) {
// write pre-action user code here
exitMIDlet();
// write post-action user code here
}
}
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: exitCommand ">
publicCommand getExitCommand() {
if (exitCommand == null) {
// write pre-init user code here
exitCommand = new Command("Exit", Command.EXIT, 0);
// write post-init user code here
}
return exitCommand;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: form ">
public FormgetForm() {
if (form == null) {
// write pre-init user code here
form = new Form("Welcome", new Item[] { getImageItem() });
form.addCommand(getExitCommand());
form.setCommandListener(this);
// write post-init user code here
}
return form;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: imageItem ">
public ImageItem getImageItem() {
if (imageItem == null) {
// write pre-init user code here
imageItem = new ImageItem("imageItem", getImage1(),ImageItem.LAYOUT_DEFAULT,"</u7F3A/u5C11/u56FE/u50CF>");
// write post-init user code here
}
return imageItem;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: image1 ">
public Image getImage1() {
if (image1 == null) {
// write pre-init user code here
try {
image1 = Image.createImage("/Snap1.jpg");
} catch (java.io.IOException e) {
Alert alert=new Alert("错误","找不到图像文件",null,null);
alert.setTimeout(2000);
alert.setType(AlertType.ERROR);
switchDisplayable(alert,form);
}
// write post-init user code here
}
return image1;
}
//</editor-fold>
publicDisplay getDisplay () {
return Display.getDisplay(this);
}
public voidexitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public voidstartApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
public voidpauseApp() {
midletPaused = true;
}
public voiddestroyApp(boolean unconditional) {
}
}