addressbookmidlet.java

来自「JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程」· Java 代码 · 共 69 行

JAVA
69
字号
package ch09.section03;

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

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);
  AddressDB dbAddress = null;
  public AddressBookMIDLet() {
    dbAddress = new AddressDB();
    //杜撰的通讯录记录
    file:
        dbAddress.addAddress("张三", "东风大街10号");
    dbAddress.addAddress("李四", "杏园小区10号楼");
    dbAddress.addAddress("王五", "洞达界8号");
    dbAddress.addAddress("刘的华", "巴黎巷13号");
    dbAddress.addAddress("张刚", "和平门114号");
  }

//程序启动时,加载初始化的记录
  public void startApp() throws MIDletStateChangeException {
    display = Display.getDisplay(this);
    mnuMain = new List("地址", 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 =
          AddressDB.getName(select.getSelectedIndex() + 1) + ", " +
          dbAddress.getAddress(select.getSelectedIndex() + 1);
      txtAddress = new TextBox("地址", txtSelect, 255,
                               TextField.ANY);
      txtAddress.addCommand(cmdBack);
      txtAddress.setCommandListener(this);
      display.setCurrent(txtAddress);
    }
  }
}

⌨️ 快捷键说明

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