J2ME 採用命令模式處理用戶交互:定義 Command → 加入 Displayable → 實作 CommandListener → commandAction() 統一分派。
Command backCmd = new Command("Back", Command.BACK, 2);
Command cancelCmd = new Command("Cancel", Command.CANCEL, 1);
Command exitCmd = new Command("Exit", Command.EXIT, 1);
Command helpCmd = new Command("Help", Command.HELP, 1);
Command itemCmd = new Command("Item", Command.ITEM, 1);
Command okCmd = new Command("OK", Command.OK, 1);
Command screenCmd = new Command("Screen", Command.SCREEN, 1);
Command stopCmd = new Command("Stop", Command.STOP, 1);
public void commandAction(Command c, Displayable d) {
if (c == exitCmd) {
destroyApp(false); notifyDestroyed();
} else if (c == helpCmd) { showScreen("Help"); }
else if (c == backCmd) { showScreen("Back"); }
else if (c == cancelCmd) { showScreen("Cancel"); }
else if (c == itemCmd) { showScreen("Item"); }
else if (c == okCmd) { showScreen("OK"); }
else if (c == screenCmd) { showScreen("Screen"); }
else if (c == stopCmd) { showScreen("Stop"); }
}
| 元件 | 類型 | 行數 | 可獨立顯示 | 用途 |
|---|---|---|---|---|
| TextBox | Screen (Displayable) | 多行 | 是 | 全螢幕文字編輯 |
| TextField | Item | 單行 | 否 (需放入 Form) | 表單欄位輸入 |
| DateField | Item | 日期/時間選擇器 | 否 (需放入 Form) | 日期時間輸入 |
TextBox txtBox = new TextBox("標題", null, 200, TextField.ANY);
// null 表示無初始內容,最大長度 200,ANY 無限制
// 常見限制:EMAILADDR, NUMERIC, PHONENUMBER, URL, DECIMAL
Form form = new Form("註冊");
TextField emailField = new TextField("Email:", "", 50, TextField.EMAILADDR);
TextField pwdField = new TextField("密碼:", "", 20, TextField.PASSWORD);
TextField phoneField = new TextField("電話:", "", 20, TextField.PHONENUMBER);
form.append(emailField);
form.append(pwdField);
form.append(phoneField);
關鍵:TextField 建構子第 4 參數指定輸入模式,裝置會自動彈出對應鍵盤/輸入法。
DateField dateField = new DateField("生日:", DateField.DATE); // 僅日期
DateField dateTimeField = new DateField("約會:", DateField.DATE_TIME); // 日期+時間
DateField timeField = new DateField("鬧鐘:", DateField.TIME); // 僅時間
form.append(dateField);
// 取得值
Date selectedDate = dateField.getDate(); // 回傳 java.util.Date 或 null
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class RegistrationForm extends MIDlet implements CommandListener {
private Display display;
private Form form;
private TextField nameField, emailField, phoneField;
private DateField birthField;
private Command submitCmd = new Command("提交", Command.OK, 1);
private Command exitCmd = new Command("離開", Command.EXIT, 1);
protected void startApp() {
display = Display.getDisplay(this);
form = new Form("用戶註冊");
nameField = new TextField("姓名:", "", 30, TextField.ANY);
emailField = new TextField("Email:", "", 50, TextField.EMAILADDR);
phoneField = new TextField("電話:", "", 20, TextField.PHONENUMBER);
birthField = new DateField("生日:", DateField.DATE);
form.append(nameField);
form.append(emailField);
form.append(phoneField);
form.append(birthField);
form.addCommand(submitCmd);
form.addCommand(exitCmd);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == exitCmd) {
destroyApp(false); notifyDestroyed();
} else if (c == submitCmd) {
String name = nameField.getString();
String email = emailField.getString();
String phone = phoneField.getString();
Date birth = birthField.getDate();
// 簡單驗證
if (name.length() == 0) { showAlert("請輸入姓名"); return; }
if (!email.contains("@")) { showAlert("Email 格式錯誤"); return; }
// 顯示提交結果
Form result = new Form("註冊成功");
result.append("姓名: " + name + "\n");
result.append("Email: " + email + "\n");
result.append("電話: " + phone + "\n");
result.append("生日: " + (birth != null ? birth.toString() : "未填") + "\n");
result.addCommand(exitCmd);
display.setCurrent(result);
}
}
private void showAlert(String msg) {
Alert alert = new Alert("錯誤", msg, null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, form);
}
// ... pauseApp, destroyApp 標準實作
}