服务热线:13616026886

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

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

j2me学习笔记(4)―用midp api开发midlets


  1.midp中的特定类和接口
  1) javax.microedition.midlet-----是最内层的包,只包含一个midlet类,它为midp应用提供了基本功能开支
  
  2) javax.microedition.io-----包括httpconnection接口,通过这个接口,midlet设备可访问web中的数据
  
  3) javax.microedition.lcdui(liquid crystal display user interface液晶显示用户界面)-----对gui组件提供支持,有与java.awt同样的功能,javax.microedition.lcdui包中的类和接口更适用与小屏幕的手持设备.
  
  4) javax.microedition.rms(记录管理系统)-----支持数据库系统的不同类和接口,它们随后存储和检索midlet所用的数据
  
  2. 一个实例:
  1) 任务陈述-----savemymoney银行的移动应用系统第一个屏幕显示欢迎消息,第二个屏幕上应该显示当前日期,当前时间,总的内存,以及自由内存
  
  2) 实现代码如下:
  
  import javax.microedition.midlet.*;
  
  import javax.microedition.lcdui.*;
  
  import java.util.*;
  
  public class mb extends midlet implements commandlistener
  
  {
      display display;
  
      form form1;
  
      form form2;
  
      ticker ticker1;
  
      static final command okcommand = new command("info",command.ok,1);
  
      static final command backcommand = new command("back",command.back,0);
  
      static final command exitcommand = new command("exit", command.stop, 2);
  
      public mb()
  
      {
  
      }
  
      public void startapp() throws midletstatechangeexception
  
      {
         display = display.getdisplay(this);
  
         ticker1=new ticker("welcome to the world of mobile banking!");
  
         form1 = new form("savemymoney");
  
         form2 = new form("information");
  
  
  
  calendar calendar1 = calendar.getinstance();
  
  int date=calendar1.get(calendar.month);
  
        string date1 = integer.tostring (calendar1.get(calendar.day_of_month)) + " " +
  
                  integer.tostring(calendar1.get(calendar.year));
  
   string time = integer.tostring(calendar1.get(calendar.hour_of_day)) + ":" +
  
           integer.tostring(calendar1.get(calendar.minute)) + ":" +
  
           integer.tostring(calendar1.get(calendar.second));
  
  runtime runmemory = runtime.getruntime();
  
        string total_memory = long.tostring(runmemory.totalmemory());
  
         string free_memory = long.tostring(runmemory.freememory());
  
  string month =" 0";
  
  switch(date)
  
  {
             case 0: month="january";break;
  
             case 1: month="february";break;
  
             case 2: month="march";break;
  
             case 3: month="april";break;
  
             case 4: month="may";break;
  
             case 5: month="june";break;
  
             case 6: month="july";break;
  
             case 7: month="august";break;
  
             case 8: month="september";break;
  
             case 9: month="october";break;
  
             case 10: month="november";break;
  
             case 11: month="december";break;
  
  }
         stringitem stritem = new stringitem("welcome to  savemymoney bank!", "");
  
         form1.append(stritem);
  
         form2.append(new stringitem(" ", "date:         "+ month +",")) ;
  
         form2.append(date1);
  
         form2.append(new stringitem(" ", "time:         " + time));
  
         form2.append(new stringitem(" ", "total memory:   " + total_memory));       
  
         form2.append(new stringitem(" ", "free memory:   " + free_memory));
  
         form1.addcommand(exitcommand);
  
         form1.addcommand(okcommand);
  
         form1.setcommandlistener(this);
  
         form1.setticker(ticker1);
  
         display.setcurrent(form1);
  
      }
  
      public void pauseapp()
  
      {
  
      }
  
      public void destroyapp(boolean unconditional)
  
      {
  
         notifydestroyed();
  
      }
  
      public void showform1()
  
      {
  
         form1.addcommand(exitcommand);
  
         form1.addcommand(okcommand);
  
         form1.setcommandlistener(this);
  
         display.setcurrent(form1);
  
      }
  
      public void showform2()
  
      {
  
         form2.addcommand(exitcommand);
  
         form2.addcommand(backcommand);
  
         form2.setcommandlistener(this);
  
         display.setcurrent(form2);
  
      }
  
      public void commandaction(command c, displayable d)
  
      {
          string label = c.getlabel();
  
          if (label.equals("exit"))
  
          {
  
              destroyapp(true);
  
          }
  
          else if (label.equals("back"))
  
          {
  
               // go back to form1
  
                 showform1();
  
            }
  
          else
  
         {
  
             showform2();
         }
     }
  }

扫描关注微信公众号