当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

Java环境中封装多MIDIet程序


  你可以将多个MIDIet程序存储在一个JAR文件中。设备会自动显示一个MIDIet的菜单,又用户选择运行哪个MIDIet程序。
  
  以下的例子中包含所有你所需要的: 两个简单MIDIet程序的源代码,一个存储在JAR文件中的表单文件(MANIFEST),一个JAVA文件描述符文件(.jad),MIDIet的图象文件(.png),还有一个用于运行MIDIet程序和编译,预检,创建JAR文件的DOS批处理文件。
  
  源代码1:
  /*----------------------------------------------------
  * www.CoreJ2ME.com
  *
  * 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:
  
  /*----------------------------------------------------
  * www.CoreJ2ME.com
  *
  * 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
  

相关内容
赞助商链接