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

来源:互联网 发布:网络用语jd是什么意思 编辑:程序博客网 时间:2024/04/30 14:07

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

              }

      }

}

 

原创粉丝点击