addressbookmidlet.java

来自「本光盘是《J2ME无线移动游戏开发》一书的配套光盘」· Java 代码 · 共 66 行

JAVA
66
字号
package ch12;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class AddressBookMIDLet
    extends MIDlet
    implements CommandListener {
  Display display = null;
  List mnuMain = null;
  TextBox txtAddress = null;
  static final Command cmdBack = new Command("返回", Command.BACK, 0);
  static final Command cmdExit = new Command("退出", Command.STOP, 3);
  AddressBookDB dbAddress = null;
  public AddressBookMIDLet() {
    dbAddress = new AddressBookDB();
    //杜撰的地址
    dbAddress.addAddress("Bill Gates", "123 Elm Street");
    dbAddress.addAddress("George Bush", "742 Avenue B");
    dbAddress.addAddress("Yuki Aka", "853 Franklin Avenue");
    dbAddress.addAddress("Oba Muchow", "101 Scenic Highway");
    dbAddress.addAddress("Bill Clinton", "741 Highway 101");
  }

  public void startApp() throws MIDletStateChangeException {
    display = Display.getDisplay(this);
    mnuMain = new List("Addresses", Choice.IMPLICIT);
    int count = dbAddress.recordCount();
    for (int i = 0; i < count; i++) {
      mnuMain.append(dbAddress.getName(i + 1), null);
    }
    mnuMain.addCommand(cmdExit);
    mnuMain.setCommandListener(this);
    display.setCurrent(mnuMain);
  }

  public void pauseApp() {
    display = null;
  }

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

  public void commandAction(Command c, Displayable d) {
    String str = c.getLabel();
    if (str.equals("退出")) {
      destroyApp(true);
    }
    else if (str.equals("返回")) {
      display.setCurrent(mnuMain);
    }
    else {
      List select = (List) display.getCurrent();
      String txtSelect =
          AddressBookDB.getName(select.getSelectedIndex() + 1) + ", " +
          dbAddress.getAddress(select.getSelectedIndex() + 1);
      txtAddress = new TextBox("Address", txtSelect, 255,
                               TextField.ANY);
      txtAddress.addCommand(cmdBack);
      txtAddress.setCommandListener(this);
      display.setCurrent(txtAddress);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?