---
title: "第一个MIDP程序"
author: "mmdev"
date: "2004-07-06T20:58:00Z"
tags: []
slug: "midp"
draft: false
lastmod: "2004-07-06T20:58:00Z"
id: 1006740
url: "https://mmdev.iteye.com/blog/1006740"
---

这是一个最简单的MIDP程序，可以当作模版：

> **import** javax.microedition.midlet.\*;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;
> 
> **import** javax.microedition.lcdui.\*;
> 
> **public** **class** HelloMIDlet **extends** MIDlet **implements** CommandListener
> 
> {
> 
> **private** Display display;
> 
> **private** Command exitCommand;
> 
> **private** TextBox text;
> 
> **public** HelloMIDlet() {
> 
> display = Display.getDisplay(**this**);
> 
> exitCommand = **new** Command("离开", Command.BACK, 1);
> 
> text = **new** TextBox("Hello App", "Welcome!", 256, 0);
> 
> text.addCommand(exitCommand);
> 
> text.setCommandListener(**this**);
> 
> }
> 
> **public** **void** startApp() {
> 
> display.setCurrent(text);
> 
> }
> 
> **public** **void** pauseApp() {}
> 
> **public** **void** destroyApp(**boolean** unconditional) {}
> 
> **public** **void** commandAction(Command c, Displayable s) {
> 
> **if** (c == exitCommand) {
> 
> destroyApp(**false**);
> 
> notifyDestroyed();
> 
> }
> 
> }
> 
> }

MIDP的UI继承体系如下：

![](/images/blog_csdn_net/asklxf/16543/o_midp_heritage.jpg)

其中Canvas是低级接口，用于游戏等需要直接绘图的程序，Screen是高级接口，适合普通窗口程序如电话簿等。