- UID
- 872238
|
一个简单的例子
以下是一个HellwoWorld类型的简单例子,它可让你看看一个midlet的生命周期,midlet是在你的移动设备上执行的代码名字。它与一个applet类似,它包含有用户界面、数据和控制能力。
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMidlet extends MIDlet implements CommandListener
{
// Initialize the Midlet Display variable
private Display midletDisplay;
// Initialize a variable for the doneCommand
private Command doneCommand;
public HelloMidlet()
{
// Retrieve the display from the static display object
midletDisplay = Display.getDisplay(this);
// Initialize the doneCommand
doneCommand = new Command("DONE", Command.SCREEN, 1);
}
/**
* Create the Hello Midlet World TextBox and associate
* the exit command and listener.
*/
public void startApp()
{
// Create the TextBox containing the "Hello Midlet World!!" message
TextBox textBox = new TextBox("Hello Midlet", "Hello Midlet World!!", 256, 0);
// Add the done Command to the TextBox
textBox.addCommand(doneCommand);
// Set the command listener for the textbox to the current midlet
textBox.setCommandListener( (CommandListener) this);
// Set the current display of the midlet to the textBox screen
midletDisplay.setCurrent(textBox);
}
/**
* PauseApp is used to suspend background activities and release
* resources on the device when the midlet is not active.
*/
public void pauseApp()
{
}
/**
* DestroyApp is used to stop background activities and release
* resources on the device when the midlet is at the end of its
* life cycle.
*/
public void destroyApp(boolean unconditional)
{
}
/*
* The commandAction method is implemented by this midlet to
* satisfy the CommandListener interface and handle the done action.
*/
public void commandAction(Command command, Displayable screen)
{
// If the command is the doneCommand
if (command == doneCommand)
{
// Call the destroyApp method
destroyApp(false);
// Notify the midlet platform that the midlet has completed
notifyDestroyed();
}
}
}
|
midlet的输出如图一所示,它其实是J2ME Windows Toolkit DefaultGrayPhone模拟器的一张抓图。 |
|