4.3.1 Hello,World
经过那个漫长的(但确实是有趣的)介绍,我们最后终于可以真正开始编写代码了。
首先在j2me home中建立一个projects目录来保存自己的项目。在projects目录中建立一个hello的子目录。如果愿意也可以采用别的名字,本书的其他例子也是这样。
像大多数开发语言那样,我们以Hello,world的MIDlet为例子。第一个MIDlet的代码就是这样的。创建一个名叫hello.java的文件来保存代码,也可以在光盘的source code for Chapter 4中找到这些代码。
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
/**
* A simple example that demonstrates the basic structure of a MIDlet
* by creating a Form object containing the string "Hello, Micro World!"
*
* @author Martin J. Wells
*/
public class Hello extends javax.microedition.midlet.MIDlet
implements CommandListener
{
protected Form form;
protected Command quit;
/**
* Constructor for the MIDlet which instantiates the Form object and
* then adds a text message. It then sets up a command listener so it
* will get called back when the user hits the quit command. Note that this
* Form is not activated (displayed) until the startApp method is called.
*/
public Hello()
{
// create a form and add our components
form = new Form("My Midlet");
form.append("Hello, Micro World!");
// create a way to quit
form.setCommandListener(this);
quit = new Command("Quit", Command.SCREEN, 1);
form.addCommand(quit);
}
/**
* Called by the Application Manager when the MIDlet is starting or resuming
* after being paused. In this example it acquires the current Display object
* and uses it to set the Form object created in the MIDlet constructor as
* the active Screen to display.
* @throws MIDletStateChangeException
*/
protected void startApp() throws MIDletStateChangeException
{
// display our form
Display.getDisplay(this).setCurrent(form);
}
/**
* Called by the MID's Application Manager to pause the MIDlet. A good
* example of this is when the user receives an incoming phone call while
* playing your game. When they're done the Application Manager will call
* startApp to resume. For this example we don't need to do anything.
*/
protected void pauseApp()
{
}
/**
* Called by the MID's Application Manager when the MIDlet is about to
* be destroyed (removed from memory). You should take this as an opportunity
* to clear up any resources and save the game. For this example we don't need
* to do anything.
* @param unconditional if false you have the option of throwing a
* MIDletStateChangeException to abort the destruction process.
* @throws MIDletStateChangeException
*/
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException
{
}
/**
* The CommandListener interface method called when the user executes
* a Command, in this case it can only be the quit command we created in the
* constructor and added to the Form.
* @param command
* @param displayable
*/
public void commandAction(Command command, Displayable displayable)
{
// check for our quit command and act accordingly
try
{
if (command == quit)
{
destroyApp(true);
// tell the Application Manager we're exiting
notifyDestroyed();
}
}
// we catch this even though there's no chance it will be thrown
// since we called destroyApp with unconditional set to true.
catch (MIDletStateChangeException me)
{ }
}
}
不要过于关注这个例子中的太多细节,因为这个例子并不是最精简的。其中第一个MIDlet包含较少的组件,后文将仔细研究这些组件。






