![]() |
|
|
作者:Yuan, Mi… 文章来源:IBM
Eclipse 的新的类向导 尽管 Nokia 插件为生成新的类在 Tools > Nokia Developer's Suite for J2ME中提供了一个向导,但我更喜欢使用 Eclipse 的默认向导,因为它支持超类和已实现接口的自动代码完成(automatic code completion)。 ![]() 图 13. 新类 从该向导中生成的主要 Java 源代码文件如下所示: ![]() 图 14. 生成的源代码 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) { 上一页12 3 4 下一页
|