你可以将多个MIDIet程序存储在一个JAR文件中。设备会自动显示一个MIDIet的菜单,又用户选择运行哪个MIDIet程序。

  以下的例子中包含所有你所需要的:两个简单MIDIet程序的源代码,一个存储在JAR文件中的表单文件(MANIFEST),一个JAVA文件描述符文件(.jad),MIDIet的图象文件(.png),还有一个用于运行MIDIet程序和编译,预检,创建JAR文件的DOS批处理文件。
 

源代码1:

/*----------------------------------------------------
 * Simple MIDlet1
 *---------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MIDlet1 extends MIDlet implements CommandListener
{
 private Display display; // Reference to Display object
 private TextBox tbxMain; // A Textbox to display a message
 private Command cmdExit; // A Command to exit the MIDlet

// The constructor
 public MIDlet1()
 {
  display = Display.getDisplay(this);

  cmdExit = new Command("Exit", Command.SCREEN, 1);

  tbxMain = new TextBox("Welcome", "Core J2ME", 50, 0);
  tbxMain.addCommand(cmdExit);
  tbxMain.setCommandListener(this);
 }

// Called by application manager to start the MIDlet.
 public void startApp()
 {
  display.setCurrent(tbxMain);
 }

// A required method
 public void pauseApp()
 { }

// A required method
 public void destroyApp(boolean unconditional)
 { }

// Check to see if our Exit command was selected
 public void commandAction(Command c, Displayable s)
 {
  if (c == cmdExit)
  {
   destroyApp(false);
   notifyDestroyed();
  }
 }
}

 

源代码2:

/*----------------------------------------------------
 * Simple MIDlet2
 *---------------------------------------------------*/

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MIDlet2 extends MIDlet implements CommandListener
{
 private Display display; // Reference to Display object
 private Form frmMain; // The main form
 private TextField txfName; // A text field to prompt for name
 private Command cmdExit; // A Command to exit the MIDlet

// The constructor
 public MIDlet2()
 {
  display = Display.getDisplay(this);

  cmdExit = new Command("Exit", Command.SCREEN, 1);

  txfName = new TextField("Name:", "", 10, TextField.ANY);

  frmMain = new Form("Sample Form");
  frmMain.addCommand(cmdExit);
  frmMain.append(txfName);
  frmMain.setCommandListener(this);
 }

// Called by application manager to start the MIDlet.
 public void startApp()
 {
  display.setCurrent(frmMain);
 }

// A required method
 public void pauseApp()
 { }

// A required method
 public void destroyApp(boolean unconditional)
 { }

// Check to see if our Exit command was selected
 public void commandAction(Command c, Displayable s)
 {
  if (c == cmdExit)
  {
   destroyApp(false);
   notifyDestroyed();
  }
 }
}

  mainfest.mf 文件(要存到JAR文件里):

MIDlet-Name: TwoMIDlets
MIDlet-Version: 1.0
MIDlet-Vendor: Core J2ME Technology
MIDlet-1: MIDlet1,/image1.png, MIDlet1
MIDlet-2: MIDlet2,/image2.png, MIDlet2
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile&: MIDP-1.0

  MIDlet.jad 文件

MIDlet-Name: TwoMIDlets
MIDlet-Version: 1.0
MIDlet-Vendor: Core J2ME Technology
MIDlet-Description: Packaging multiple MIDlets
MIDlet-Jar-URL: MIDlet.jar
MIDlet-Jar-Size: 3144
MIDlet-1: MIDlet1,/image1.png, MIDlet1
MIDlet-2: MIDlet2,/image2.png, MIDlet2

  Emulator的显示结果:

  从左向右:

  1、显示MIDIet程序的菜单。

  2、运行MIDIet 1

  3、运行MIDIet 2

  文章要点:

  1. 入口:

MIDlet-1: MIDlet1,/image1.png, MIDlet1
MIDlet-2: MIDlet2,/image2.png, MIDlet2

  以上在mainfest和 jad文件中必须是同样的!

  2. 你可以省略对图像文件的引用,如下所示:

MIDlet-1: MIDlet1,, MIDlet1
MIDlet-2: MIDlet2,, MIDlet2

  3. 下面的属性在jad文件中是可选的:

MIDlet-Description: Packaging multiple MIDlets

  4. 以上的例程基于MIDP与 CLDC的1.0.3版本。