服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > 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
  
java环境中封装多midiet程序

扫描关注微信公众号