J2ME学习笔记(四)-----用特定的MIDP API开发MIDlets huanghui22 [原作]

J2ME学习笔记(四)-----用特定的MIDP API开发MIDlets
出处 

用特定的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();

              }

      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值