Alert
用於在屏幕上向用戶顯示關於異常情況或錯誤的信息。Choice
用於實現從既定數量的選項中進行選擇。ChoiceGroup
提供一組相關選項。Form
作爲其它 UI 元素的容器。List
提供一個選項列表。StringItem
充當只顯(display-only)字符串之用。TextBox
是允許用戶輸入和編輯文本的屏幕顯示。TextField
允許用戶輸入和編輯文本。多個TextField
可放到一個Form
中。DateField
是一個可編輯的組件,用於表示日期和時間信息。DateField
可以放到Form
中。Ticker
用於文本的可滾動顯示。
一個樣本應用程序:電話日曆
J2ME 的著名特色之一是它在受限環境中的日期處理功能。J2ME 提供的 DateField
UI 元素是一個可編輯的組件,該組件用於表示日曆信息(即日期和時間)。在這一部分中,我們將使用 DateField
和 Date
函數來開發一個 J2ME 應用程序,這個應用程序用於在移動電話 UI 上顯示一個滾動日曆。
// Import of API classes
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
//A first MIDlet with simple text and a few commands.
public class PhoneCalendar extends MIDlet
implements CommandListener, ItemStateListener {
//The commands
private Command exitCommand;
//The display for this MIDlet
private Display display;
// Display items e.g Form and DateField
Form displayForm;
DateField date;
public PhoneCalendar() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 1);
date = new DateField("Select to date", DateField.DATE);
}
// Start the MIDlet by creating the Form and
// associating the exit command and listener.
public void startApp() {
displayForm = new Form("Quick Calendar");
displayForm.append(date);
displayForm.addCommand(exitCommand);
displayForm.setCommandListener(this);
displayForm.setItemStateListener(this);
display.setCurrent(displayForm);
}
public void itemStateChanged(Item item)
{
// Get the values from changed item
}
// Pause is a no-op when there is no background
// activities or record stores to be closed.
public void pauseApp() { }
// Destroy must cleanup everything not handled
// by the garbage collector.
public void destroyApp (boolean unconditional) { }
// Respond to commands. Here we are only implementing
// the exit command. In the exit command, cleanup and
// notify that the MIDlet has been destroyed.
public void commandAction (
Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}