J2ME学习笔记(4)—用MIDP API开发MIDlets

2016-02-19 13:37 0 1 收藏

今天图老师小编给大家精心推荐个J2ME学习笔记(4)—用MIDP API开发MIDlets教程,一起来看看过程究竟如何进行吧!喜欢还请点个赞哦~

【 tulaoshi.com - 编程语言 】

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();
   }
   }
  }

来源:https://www.tulaoshi.com/n/20160219/1604788.html

延伸阅读
1.J2ME体系结构详解 2 Profile 2 Configuration 2 KVM(CVM) 2 Host Operating System 1)Configuration-----由于支持J2ME的硬件平台之间有很大差异 I.有比较高端的设备,例如:电视机顶盒、网络电视等-----是运算能力相对较佳、并且在电力供给上相对比较充足的嵌入式装置 II.也...
一、序言 昨天在网上闲逛,发现一篇讲解用Delphi实现华容道游戏的文章,颇受启发.于是,产生了将华容道游戏移植到手机中去的冲动.现在手机游戏琳琅满目,不一而足,华容道的实现版本也很多.正巧不久前笔者对J2ME下了一番功夫,正想借这个机会小试牛刀。选用J2ME的原因还有一个就是目前Java开发大行其到,无线增殖业务迅猛发...
J2ME Game开发笔记-尝试IO优化 正在开发的一个游戏,由于读地图的时候做了图片切割,所以速度比较慢。(在我开发上一个游戏的时候,读取地图时没有装载切割图片,速度非常快,看来IO操作的速度和createImage,drawImage相比是微不足道的)对于IO的优化也许根本不会明显的提高速度,但我还是试了一下。 分析了一下代码,在最初的...
(3).建立Draw类用来显示图形: public class Draw { /** Creates a new instance of Draw */ public Draw(Canvas canvas) { } public static boolean paint(Graphics g, byte img, int x, int y) { //在地图的x,y点绘制img指定的图片 try { paint(g, img, x, y, Ima...
概述 目前,很多手机已经具备了蓝牙功能。虽然MIDP2.0没有包括蓝牙API,但是JCP定义了JSR82, Java APIs for Bluetooth Wireless Technology (JABWT).这是一个可选API,很多支持MIDP2.0的手机已经实现了,比如Nokia 6600, Nokia 6670,Nokia7610等等。对于一个开发者来说,假如目标平台支...

经验教程

229

收藏

53
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部