首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

J2ME 學習筆記(三):MIDlet 生命週期與銀行應用實作範例

     編輯:Java 程式設計入門

前言

MIDlet 是 J2ME 應用程式的核心單位。本文說明 MIDlet 打包形式、執行環境、生命週期三狀態,並以「SaveMyMoney 行動銀行」雙畫面範例完整示範 UI 建構、命令處理、畫面切換。

一、MIDlet 基礎認知

  • 打包為 JAD (Java Application Descriptor) + JAR。
  • 運行於裝置上的 AMS (Application Management Software),提供 KVM 與 MIDlet 執行環境。
  • 適用於支援 CLDC 與 MIDP 的手持裝置。

二、生命週期三大狀態與轉換

  1. Paused (暫停):初始狀態,或由 Active 轉入。資源應釋放。
  2. Active (執行中):startApp() 進入,UI 顯示、網路運作。可轉入 Paused 或 Destroyed。
  3. Destroyed (銷毀):destroyApp() 進入,釋放所有資源,呼叫 notifyDestroyed() 通知 AMS。

狀態轉換由 AMS 控制,MIDlet 亦可主動請求(如呼叫 notifyPaused())。

三、實作範例:雙畫面銀行應用

需求

  • 畫面一:顯示「Welcome to SaveMyMoney Bank!」,跑馬燈「Welcome to the World of Mobile Banking!」,右鍵「Info」進入畫面二,左鍵「Exit」離開。
  • 畫面二:顯示客戶服務文字,跑馬燈「Note: Your PIN number has been sent...」,左鍵「Back」返回,右鍵「Exit」離開。

核心程式碼

public class MB extends MIDlet implements CommandListener {
    private Display display;
    private Form form1, form2;
    private Ticker ticker1, ticker2;
    private static final Command okCmd = new Command("Info", Command.OK, 1);
    private static final Command backCmd = new Command("Back", Command.BACK, 0);
    private static final Command exitCmd = new Command("Exit", Command.STOP, 2);

    public void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        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.");

        form1 = new Form("SaveMyMoney");
        form1.append(new StringItem("Welcome to SaveMyMoney Bank!", ""));
        form1.addCommand(exitCmd);
        form1.addCommand(okCmd);
        form1.setCommandListener(this);
        form1.setTicker(ticker1);

        form2 = new Form("CUSTOMER CARE");
        form2.append(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."));
        form2.addCommand(exitCmd);
        form2.addCommand(backCmd);
        form2.setCommandListener(this);
        form2.setTicker(ticker2);

        display.setCurrent(form1);
    }

    public void pauseApp() { /* 釋放資源 */ }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
        String label = c.getLabel();
        if ("Exit".equals(label)) {
            destroyApp(true);
        } else if ("Back".equals(label)) {
            display.setCurrent(form1);
        } else { // Info
            display.setCurrent(form2);
        }
    }
}

關鍵技術點

  • Display.getDisplay(this) 取得顯示管理器。
  • Form 為容器,StringItem 顯示唯讀文字,Ticker 實現跑馬燈。
  • Command 封裝語意(標籤、類型、優先權),對應軟鍵。
  • CommandListener 集中處理命令事件,依標籤分派邏輯。
  • 畫面切換以 display.setCurrent() 實現。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved