首页 | 旅游 | 健康 | 时尚 | 下载 | 论坛 | 图文 | 专题 | 地图
资讯 IT人 电脑入门 操作系统 上网 办公 技巧 硬件 软件 网络 图像 多媒体 程序 数据库 网页制作 网站开发 网游 安全 加密 企业

使用Eclipse为Nokia手机开发移动应用程序(2)

巧巧读书 2006-10-16 IBM Yuan, Mi… 技术论坛

使用Eclipse为Nokia手机开发移动应用程序(2)(图一)

Eclipse 的新的类向导


尽管 Nokia 插件为生成新的类在 Tools > Nokia Developer's Suite for J2ME中提供了一个向导,但我更喜欢使用 Eclipse 的默认向导,因为它支持超类和已实现接口的自动代码完成(automatic code completion)。

图 13. 新类
使用Eclipse为Nokia手机开发移动应用程序(2)(图二)

从该向导中生成的主要 Java 源代码文件如下所示:

图 14. 生成的源代码
使用Eclipse为Nokia手机开发移动应用程序(2)(图三)

TutorialMidlet 类


TutorialMidlet 类是为应用程序提供输入执行的类。Java 运行时环境(Java Runtime Environment,JRE)首先会实例化这个类,然后调用其 startApp() 方法启动 midlet。在用户终止应用程序时,可以调用 destroyApp() 方法。

TutorialMidlet 类控制并显示应用程序中的所有 UI 屏幕。所有用户生成的软键(soft-key)事件(比如用户按下一个软键时)都由 TutorialMidlet 类处理,因为它实现了 CommandListener 接口,并将自己作为所有屏幕对象的命令监听程序附加到该接口上。UI 事件回调方法是 commandAction()


package tutorial;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class TutorialMidlet extends MIDlet
                          implements CommandListener {

  Display display;
  Command greetCommand;
  Command exitCommand;
  Command clearCommand;
  Command backCommand;
  WelcomeScreen welcomeScreen;
  HelloScreen helloScreen;

  // instantiate the internal variables
  public TutorialMidlet () {
    display = Display.getDisplay(this);
    greetCommand =
        new Command ("Greet", Command.OK, 0);
    exitCommand =
        new Command ("Exit", Command.EXIT, 0);
    clearCommand =
        new Command ("Clear", Command.CANCEL, 1);
    backCommand =
        new Command ("Back", Command.SCREEN, 1);

    welcomeScreen = new WelcomeScreen ();
    welcomeScreen.addCommand (greetCommand);
    welcomeScreen.addCommand (clearCommand);
    welcomeScreen.setCommandListener (this);

    helloScreen = new HelloScreen ();
    helloScreen.addCommand (exitCommand);
    helloScreen.addCommand (backCommand);
    helloScreen.setCommandListener (this);
  }

  // Called when the MIDlet is started by the AMS
  protected void startApp () {
    display.setCurrent (welcomeScreen);
  }

  protected void pauseApp () {
    // Do nothing
  }

  protected void destroyApp (boolean unconditional) {
    notifyDestroyed ();
  }

  public void commandAction (Command c, Displayable d) {
    if (c == greetCommand) {
      String name = welcomeScreen.getName ();
      helloScreen.setName(name);
      display.setCurrent (helloScreen);

    } else if (c == clearCommand) {
      welcomeScreen.setName("");
      display.setCurrent(welcomeScreen);

    } else if (c == backCommand) {
      display.setCurrent (welcomeScreen);

    } else if (c == exitCommand) {
      destroyApp (true);
    }
  }
}
实时错误检验


TutorialMidlet 类使用 WelcomeScreenHelloScreen 类,这些类您也必须键入。

Eclipse 中的高级 Java 编辑器会用红色线条(red bar)标出相关的代码路径,警告您存在这类冲突。

如果您将鼠标放在红线上,编辑器会显示一个解释框,告诉您为什么它认为这是一个错误。

实时语法检验允许开发人员利用 Java 编译器的一致性检验功能,而不用实际等待编译的完成。

图 15. 实时错误检验

使用Eclipse为Nokia手机开发移动应用程序(2)(图四)

如果选定 Eclipse Project 菜单中的 Build automatically 选项,

那么每当更新项目时,Eclipse 都会试着不断在后台构建该项目。

在这种情况下,Package Explorer 也会显示在构建过程中检测到的编译错误。

图 16. 自动构建错误

使用Eclipse为Nokia手机开发移动应用程序(2)(图五) 

WelcomeScreen 类


WelcomeScreen 类扩展了 Form 类来展示包含一些高级 MIDP UI 组件的屏幕。

正如您已经看到的,WelcomeScreen 的软键命令和事件处理程序

被添加在 TutorialMidlet 类中。可以通过 JavaBeans 风格的 getset 方法,

从这个类的外部访问 nameField UI 组件中的数据。

以这种形式使用的图像来自外部的 PNG 图像文件 welcome.png

该文件必须位于 midlet 的运行时类路径中,以便对它进行访问。


package tutorial;

import javax.microedition.lcdui.*;

public class WelcomeScreen extends Form {

  private TextField nameField;

  public WelcomeScreen () {
    super ("Welcome");

    Image img;
    // Construct the image from the media file
    try {
      img = Image.createImage("/welcome.png");
    } catch (Exception e) {
      e.printStackTrace ();
      img = null;
    }
    ImageItem imageItem =
        new ImageItem ("", img,
              ImageItem.LAYOUT_CENTER, "Welcome");

    nameField =
        new TextField ("Please enter your name", "",
                       10, TextField.ANY);

    append (imageItem);
    append (nameField);
  }

  public void setName (String n) {
    nameField.setString (n);
  }

  public String getName () {
    return nameField.getString ();
  }
}

HelloScreen 类


HelloScreen 类扩展了Canvas 类来展示必须通过应用程序自身得到渲染的屏幕。

paint() 方法重新绘制了整个屏幕,每次屏幕需要更新时,都由系统调用该方法。

再次声明,HelloScreen 的软键命令和事件处理程序被添加在 TutorialMidlet 类中。

屏幕上渲染的名称字符串是通过 setName() 方法在显示该屏幕之前设置的。

图像文件 hello.png 也必须位于 midlet 运行时类路径中。


package tutorial;

import javax.microedition.lcdui.*;

public class HelloScreen extends Canvas {

  private int width, height;
  private String name;
  private Image img;

  public HelloScreen () {
    width = getWidth ();
    height = getHeight ();
    name = "unknown";

    // Construct the image from the media file
    try {
      img = Image.createImage("/hello.png");
    } catch (Exception e) {
      e.printStackTrace ();
      img = null;
    }
  }

  public void setName (String n) {
    name = n;
  }

  // Paint the screen based on the name
  protected void paint (Graphics g) {
    g.setColor(0xffffff);
    g.fillRect(0,  0, width, height);
    g.setColor(0x000000);

    g.drawImage (img, width / 2, height / 4,
            Graphics.VCENTER | Graphics.HCENTER);

    g.setFont(Font.getFont(
                Font.FACE_PROPORTIONAL,
                Font.STYLE_BOLD,
                Font.SIZE_LARGE));
    g.drawString (name, width / 2, height * 3/4,
            Graphics.BASELINE | Graphics.HCENTER);
  }
}

不再有错误


键入所有的源代码,如果 Eclipse 没有显示任何错误或不一致,

您就已经为运行 midlet 做好了准备!

图 17. 没有错误,运行 midlet

使用Eclipse为Nokia手机开发移动应用程序(2)(图六)

上一个话题:Spring中定义Bean
本类最热图文
巧巧读书图文推荐
Google