J2ME MIDP提供的圖形元素 選擇自 lastking 的 Blog

MIDP API 儘管維護的是一個受限的框架,但它還是提供了 UI 元素的完整集合。以下是最重要的 UI 元素中的一些:
  • Alert 用於在屏幕上向用戶顯示關於異常情況或錯誤的信息。

  • Choice 用於實現從既定數量的選項中進行選擇。

  • ChoiceGroup 提供一組相關選項。

  • Form 作爲其它 UI 元素的容器。

  • List 提供一個選項列表。

  • StringItem 充當只顯(display-only)字符串之用。

  • TextBox 是允許用戶輸入和編輯文本的屏幕顯示。

  • TextField 允許用戶輸入和編輯文本。多個 TextField 可放到一個 Form 中。

  • DateField 是一個可編輯的組件,用於表示日期和時間信息。DateField 可以放到 Form 中。

  • Ticker 用於文本的可滾動顯示。

一個樣本應用程序:電話日曆
J2ME 的著名特色之一是它在受限環境中的日期處理功能。J2ME 提供的 DateField UI 元素是一個可編輯的組件,該組件用於表示日曆信息(即日期和時間)。在這一部分中,我們將使用 DateFieldDate 函數來開發一個 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();
}
}


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章