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

J2ME學習筆記(3)—初次接觸MIDlets

編輯:JAVA編程入門知識
  1.MIDlet是使用MIDP特征和CLDC配置的MIDlet應用
  1).MIDlet是打包成JAD(Java描述符)文件的Java類文件
  
  2).MIDlet運行在已安裝於MIDP設備上的Application Management Software(應用治理軟件AMS).AMS提供KVM和MIDlets的環境
  
  3).MIDlet是在支持CLDC和MIDP的手持設備中使用.
  
  2.MIDlet的生命周期
  
  3.開發MIDlets實例
  1).任務陳述-----SaveMyMoney移動銀行應用的第一個屏幕上要顯示的消息為”Welcome to  SaveMyMoney Bank!”,屏幕頂部有一個顯示消息"Welcome to the World of Mobile Banking!"的滾動文本;
  
  第二個屏幕上要顯示的消息為"Dear Customer,  , You can view your personal account information by entering your PIN number and sending it to the number 9002. If you have not received the PIN number, please contact us at our Head Office."屏幕頂部有一個顯示消息"Note: Your PIN number has been sent to you at your mailing address."的滾動文本
  
  2).代碼如下-----
  
  import javax.microedition.midlet.*;
  
  import javax.microedition.lcdui.*;
  
  //需要實現lcdui類中的CommandListener接口
  
  public class MB extends MIDlet implements CommandListener
  
  {
  
    //用Display類治理顯示和用戶的輸入
  
    Display display;
  
    Form form1;
  
    Form form2;
  
    //定義兩個滾動條ticker1,ticker2
  
    Ticker ticker1;
  
    Ticker ticker2;
  
    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
  
    {
  
   ticker1 = new Ticker("Welcome to the World of Mobile Banking!");
  
   ticker2 = new Ticker("Note: Your PIN number has been sent to you at your mailing address.");
  
   display = Display.getDisplay(this);
  
   form1 = new Form("SaveMyMoney");
  
   form2 = new Form("CUSTOMER CARE");
  
   StringItem strItem = new StringItem("Welcome to  SaveMyMoney Bank!", "");
  
   StringItem strItem1 = new StringItem("Dear Customer,  ", "You can view your personal account information by entering your PIN number and sending it to the number 9002. If you have not received the PIN number, please contact us at our Head Office.");
  
   form1.append(strItem);
  
   form2.append(strItem1);
  
   //把命令加入到屏幕,第一個屏幕的左軟鍵是Exit,右軟鍵是OK
  
   form1.addCommand(exitCommand);
  
   form1.addCommand(okCommand);
  
   //監聽
  
   form1.setCommandListener(this);
  
   form1.setTicker(ticker1);
  
   //設置主屏幕的當前顯示為Form1
  
   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);
  
   form2.setTicker(ticker2);
  
   display.setCurrent(form2);
  
    }
  
    public void commandAction(Command cmd, Displayable displayable)
  
    {
  
   String label = cmd.getLabel();
  
   if (label.equals("Exit"))
  
   {
  
   //調用撤消MIDlet的動作並退出此應用
  
   destroyApp(true);
  
   }
  
   else if (label.equals("Back"))
  
   {
  
  // go back to Form1
  
   showForm1();
  
   }
  
   else
  
   {
  showForm2();
  
   }
    }
  }
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved