This section provides you with the source code for the two classes needed in the HelloWorld example.
Note: All files in this example contain the Nokia copyright statement.
The functionality of the HelloWorldMIDlet
is very
simple. The startApp()
method sets the MIDlet’s current
Display to with a HelloScreen
object if a current display
does not already exist. If the HelloScreen
receives an
“Exit” command from the user interface, it calls the exitRequested()
method
of the parent HelloWorldMIDlet
which handles the MIDlet
state transition to the ‘Destroyed’ state. The MIDlet pauseApp()
and destroyApp()
methods
are empty, because this example is so simple.
package example.hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class HelloWorldMIDlet extends MIDlet { public HelloWorldMIDlet() { } public void startApp() { Displayable current = Display.getDisplay(this).getCurrent(); if(current == null) { HelloScreen helloScreen = new HelloScreen(this, "Hello World."); Display.getDisplay(this).setCurrent(helloScreen); } } public void pauseApp() { } public void destroyApp(boolean b) { } // A convenience method for exiting void exitRequested() { destroyApp(false); notifyDestroyed(); } }
The class HelloScreen
extends the MIDP high-level
API class TextBox
. A TextBox
user
interface component has three visual areas: a title, a string contents area,
and a command area (for example, the “Exit” command in the figure below).
The class HelloScreen
implements the CommandListener
interface
used to receive user input from the command area.
package example.hello; import javax.microedition.lcdui.*; class HelloScreen extends TextBox implements CommandListener { private final HelloWorldMIDlet midlet; private final Command exitCommand; HelloScreen(HelloWorldMIDlet midlet, String string) { super("HelloWorldMIDlet", string, 256, 0); this.midlet = midlet; exitCommand = new Command("Exit", Command.EXIT, 1); addCommand(exitCommand); setCommandListener(this); } public void commandAction(Command c, Displayable d) { if (c == exitCommand) { midlet.exitRequested(); } } }