Java ME的Hello World
文章信息
這一個程式會使用一個字串項目跟一個exit按鈕來建立一個新的表單。
要安裝Nokia設備所需要的開發工具,見文章開始使用Java ME、安裝S60的Java ME開發工具及使用MTJ建立第一支MIDlet。
package com.example.helloworld;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class HelloWorldMidlet extends MIDlet implements CommandListener {
public HelloWorldMidlet() {
}
// Display
private Display display;
// Main form
private Form form;
// For the message
private StringItem stringItem;
// For the exit command
private Command exitCommand;
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
}
}
}
public void startApp() {
// Create form
stringItem = new StringItem("Hello", "Hello World!");
form = new Form(null, new Item[] {stringItem});
exitCommand = new Command("Exit", Command.EXIT, 1);
form.addCommand(exitCommand);
form.setCommandListener(this);
// Get display for drawning
display = Display.getDisplay(this);
display.setCurrent(form);
}
// Your MIDlet should not call pauseApp(), only system will call this life-cycle method
public void pauseApp() {
}
// Your MIDlet should not call destroyApp(), only system will call this life-cycle method
public void destroyApp(boolean unconditional) {
}
public void exitMIDlet() {
display.setCurrent(null);
notifyDestroyed();
}
}



(no comments yet)