程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> J2ME學習筆記(4)—用MIDP API開發MIDlets

J2ME學習筆記(4)—用MIDP API開發MIDlets

編輯:JAVA編程入門知識
  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();
   }
   }
  }
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved