Java ME的Hello World

这一篇是我在Hello World in Java ME的中文翻译,网址在Java ME的Hello World
为了备份,并转贴在此:

假如我们想要学习一个新的语言,第一个程式总会是传统的”Hello World!”

这一个程式会使用一个字串项目跟一个exit按钮来建立一个新的表单。

要安装Nokia设备所需要的开发工具,见文章开始使用Java ME安装S60的Java ME开发工具使用MTJ建立第一支MIDlet

Image:helloworld.png

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

}