课外天地 李树青学习天地JavaME移动开发课件 → 综合练习——利用常见Screen组件构造的记录管理程序


  共有15909人关注过本帖树形打印复制链接

主题:综合练习——利用常见Screen组件构造的记录管理程序

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
综合练习——利用常见Screen组件构造的记录管理程序  发帖心情 Post By:2008/9/23 20:21:24 [只看该作者]

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编辑过]

 回到顶部