以下的例子中包含所有你所需要的:兩個簡略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版本。