以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  综合练习——利用常见Screen组件构造的记录管理程序  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=538)

--  作者:admin
--  发布时间:2008/9/23 20:21:24
--  综合练习——利用常见Screen组件构造的记录管理程序

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet implements CommandListener {

        Display display;
        List list;
        TextBox textBoxNew = new TextBox("信息", "", 100, TextField.ANY);
        TextBox textBoxBrowse = new TextBox("信息", "", 100, TextField.UNEDITABLE);
        Command cmdAppend = new Command("追加于后", Command.OK, 1);
        Command cmdInsert = new Command("插入于前", Command.OK, 1);
        Command cmdSelect = new Command("查看", Command.BACK, 1);
        Command cmdOK1 = new Command("确认", Command.OK, 1);
        Command cmdOK2 = new Command("确认", Command.OK, 1);
        Command cmdDelete = new Command("删除", Command.BACK, 1);
        Command flag;

        public void startApp() {
                display = Display.getDisplay(this);
                list = new List("主界面", Choice.IMPLICIT);
                list.addCommand(cmdAppend);
                list.addCommand(cmdInsert);
                list.addCommand(cmdSelect);
                list.setCommandListener(this);

                textBoxNew.addCommand(cmdOK1);
                textBoxNew.setCommandListener(this);

                textBoxBrowse.addCommand(cmdOK2);
                textBoxBrowse.addCommand(cmdDelete);
                textBoxBrowse.setCommandListener(this);

                display.setCurrent(list);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        public void commandAction(Command c, Displayable s) {
                if (c == cmdAppend || c == cmdInsert) {
                        flag = c;
                        textBoxNew.setString("");
                        display.setCurrent(textBoxNew);
                } else if (c == cmdOK1) {
                        int index = list.getSelectedIndex();
                        String result = textBoxNew.getString();
                        if (index < 0) {
                                list.append(result, null);
                        } else {
                                if (flag == cmdAppend)
                                        list.insert(index + 1, result, null);
                                else {
                                        list.insert(index, result, null);
                                }
                        }
                        display.setCurrent(list);
                } else if (c == cmdOK2) {
                        display.setCurrent(list);
                } else if (c == cmdSelect) {
                        int index = list.getSelectedIndex();
                        if (index < 0) {
                                Alert alert = new Alert("通知");
                                alert.setType(AlertType.INFO);
                                alert.setTimeout(1000);
                                alert.setString("没有记录");
                                display.setCurrent(alert);
                        } else {
                                textBoxBrowse.setString(list.getString(index));
                                display.setCurrent(textBoxBrowse);
                        }
                } else if (c == cmdDelete) {
                        int index = list.getSelectedIndex();
                        list.delete(index);
                        display.setCurrent(list);
                }
        }
}

[此贴子已经被作者于2010-12-12 18:29:01编辑过]