Listing A—HelloJ2ME source code
import javax.microedition.midlet.*;#
import javax.microedition.lcdui.*;
public class HelloJ2ME extends MIDlet implements CommandListener
{
private Display display;
private TextField tfHello;
private Command cmExit;
private Form fmMain;
public HelloJ2ME()
{
//Get a handle to the display object
display = Display.getDisplay(this);
//Create the main form
fmMain = new Form("HelloJ2ME");
//Create the exit command button
cmExit = new Command("Exit", Command.SCREEN,1);
//Create a single-line text field 15 characters long
//with the label "Text"
tfHello = new TextField("Text","Hello World!",15,TextField.ANY);
//Add the components to the form and set up the
//command listener
fmMain.addCommand(cmExit);
fmMain.append(tfHello);
fmMain.setCommandListener(this);
}
public void startApp()
{
//set fmMain as the active object
display.setCurrent(fmMain);
}
public void pauseApp()
{ /*app is being paused*/ }
public void destroyApp(boolean unconditional)
{ /*app is being ended*/ }
public void commandAction(Command c, Displayable s)
{
//click on the Exit button
if (c == cmExit)
{
//destroyApp must be called manually
destroyApp(false);
//ask the manager to end the app
notifyDestroyed();
}
}
}