J2ME 實戰入門:從最簡單的 MIDlet 架構出發,掌握高階 UI 元件 (TextBox, List, Command) 與 RMS 持久化儲存,完整建構一個通訊錄應用。
public class MIDlet1 extends MIDlet implements CommandListener {
private Display display;
private TextBox textBox;
private Command cmdExit;
public MIDlet1() {
display = Display.getDisplay(this);
cmdExit = new Command("Exit", Command.SCREEN, 1);
textBox = new TextBox("MIDlet 1", "Welcome", 50, 0);
textBox.addCommand(cmdExit);
textBox.setCommandListener(this);
}
protected void startApp() { display.setCurrent(textBox); }
protected void pauseApp() {}
protected void destroyApp(boolean u) {}
public void commandAction(Command c, Displayable d) {
if (c == cmdExit) { destroyApp(false); notifyDestroyed(); }
}
}
| 元件 | 用途 | 關鍵方法 |
|---|---|---|
Display | 螢幕管理單例 | getDisplay(midlet), setCurrent() |
TextBox | 多行文字輸入/顯示 | setString(), getString(), setConstraints() |
List | 選單/清單 (IMPLICIT/EXCLUSIVE/MULTIPLE) | append(), getSelectedIndex() |
Form | 容器,可放入 Item | append(Item), setItemStateListener() |
Command | 軟鍵/選單動作 | new Command(label, type, priority) |
Alert | 暫時性提示/確認對話框 | setTimeout(), setType() |
RecordStore rs = RecordStore.openRecordStore("TestRecordSet", true); // true=不存在則建立
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF("資料內容");
byte[] data = bos.toByteArray();
int recordId = rs.addRecord(data, 0, data.length); // 回傳記錄 ID (1-based)
RecordEnumeration re = rs.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
byte[] rec = re.nextRecord();
// 解析 byte[] → String/物件
}
public class AddressDB {
private static RecordStore rs;
public AddressDB() { /* openRecordStore */ }
public void addAddress(String name, String addr) { /* writeUTF + addRecord */ }
public static String getName(int index) { /* enumerateRecords + 解析 */ }
public static String getAddress(int index) { /* 同上 */ }
public static int recordCount() { return rs.getNumRecords(); }
}
public class AddressBookMIDlet extends MIDlet implements CommandListener {
private Display display;
private List mainList;
private TextBox detailBox;
private AddressDB db;
private Command cmdExit, cmdBack;
public AddressBookMIDlet() {
db = new AddressDB();
// 預設測試資料
db.addAddress("Bill Gates", "123 Elm Street");
db.addAddress("George Bush", "742 Avenue B");
// ...
}
protected void startApp() {
display = Display.getDisplay(this);
mainList = new List("Addresses", List.IMPLICIT);
for (int i=1; i<=db.recordCount(); i++) {
mainList.append(db.getName(i), null);
}
mainList.addCommand(cmdExit);
mainList.setCommandListener(this);
display.setCurrent(mainList);
}
public void commandAction(Command c, Displayable d) {
if (c == cmdExit) { destroyApp(true); }
else if (c == cmdBack) { display.setCurrent(mainList); }
else { // List 選項被選取
int idx = ((List)display.getCurrent()).getSelectedIndex() + 1;
String info = db.getName(idx) + ", " + db.getAddress(idx);
detailBox = new TextBox("Address", info, 255, TextField.ANY);
detailBox.addCommand(cmdBack);
detailBox.setCommandListener(this);
display.setCurrent(detailBox);
}
}
// pauseApp/destroyApp 略
}
將預設資料移至伺服器端文字檔,MIDlet 啟動時透過 HttpConnection 下載解析,詳見「J2ME 核心功能實戰 (二)」。