基于面向对象编程的列表管理

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.Form;
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 {
        Display display;
        MainForm mainForm;

        public Exec() {
                mainForm = new MainForm("表单", this);
                display = Display.getDisplay(this);
        }

        public void startApp() {
                display.setCurrent(mainForm);
        }

        public void pauseApp() {
                display.setCurrent(null);
        }

        public void destroyApp(boolean unconditional) {
        }

        public Display getDisplay() {
                return display;
        }
}

class MainForm extends Form implements CommandListener {
        Command cmd1 = new Command("退出", Command.BACK, 1);
        Command cmd2 = new Command("显示", Command.OK, 1);
        Exec parent;
        RecordList recordList;

        public MainForm(String title, Exec parent) {
                super(title);
                this.parent = parent;
                recordList = new RecordList("主界面", parent);
                addCommand(cmd1);
                addCommand(cmd2);
                setCommandListener(this);
        }

        public void commandAction(Command c, Displayable arg1) {
                if (c == cmd1) {
                        parent.destroyApp(false);
                        parent.notifyDestroyed();
                } else if (c == cmd2) {
                        parent.getDisplay().setCurrent(recordList);
                }
        }
}

class RecordList extends List implements CommandListener {
        Command cmdAppend = new Command("追加于后", Command.OK, 1);
        Command cmdInsert = new Command("插入于前", Command.OK, 1);
        Command cmdSelect = new Command("查看", Command.BACK, 1);
        Exec parent;
        NewTextBox newTextBox;
        BrowseTextBox browseTextBox;
        InfoAlert infoAlert = new InfoAlert();

        public RecordList(String title, Exec parent) {
                super(title, Choice.IMPLICIT);
                this.parent = parent;
                newTextBox = new NewTextBox("信息", 100, parent, this);
                browseTextBox = new BrowseTextBox("内容", 100, parent, this);
                addCommand(cmdAppend);
                addCommand(cmdInsert);
                addCommand(cmdSelect);
                setCommandListener(this);
        }

        public void commandAction(Command c, Displayable arg1) {
                if (c == cmdAppend || c == cmdInsert) {
                        newTextBox.setString("");
                        if (c == cmdAppend)
                                newTextBox.setFlag("after");
                        else
                                newTextBox.setFlag("before");
                        parent.getDisplay().setCurrent(newTextBox);
                } else if (c == cmdSelect) {
                        if (browseTextBox.update())
                                parent.getDisplay().setCurrent(browseTextBox);
                        else
                                parent.getDisplay().setCurrent(infoAlert);
                }
        }
}

class NewTextBox extends TextBox implements CommandListener {
        Command cmdOK = new Command("确认", Command.OK, 1);
        Exec parent;
        List list;
        String flag;

        public NewTextBox(String title, int size, Exec parent, List list) {
                super(title, "", size, TextField.ANY);
                this.parent = parent;
                this.list = list;
                addCommand(cmdOK);
                setCommandListener(this);
        }

        public void commandAction(Command c, Displayable arg1) {
                int index = list.getSelectedIndex();
                String result = getString();
                if (index < 0) {
                        list.append(result, null);
                } else {
                        if (flag.equals("after"))
                                list.insert(index + 1, result, null);
                        else {
                                list.insert(index, result, null);
                        }
                }
                parent.getDisplay().setCurrent(list);
        }

        public void setFlag(String flag) {
                this.flag = flag;
        }
}

class BrowseTextBox extends TextBox implements CommandListener {
        Command cmdOK = new Command("确认", Command.OK, 1);
        Command cmdDelete = new Command("删除", Command.BACK, 1);
        Exec parent;
        List list;

        public BrowseTextBox(String title, int size, Exec parent, List list) {
                super(title, "", size, TextField.UNEDITABLE);
                this.parent = parent;
                this.list = list;
                addCommand(cmdOK);
                addCommand(cmdDelete);
                setCommandListener(this);
        }

        public void commandAction(Command c, Displayable arg1) {
                if (c == cmdOK) {
                        parent.getDisplay().setCurrent(list);
                } else if (c == cmdDelete) {
                        int index = list.getSelectedIndex();
                        list.delete(index);
                        parent.getDisplay().setCurrent(list);
                }
        }

        public boolean update() {
                int index = list.getSelectedIndex();
                if (index >= 0) {
                        setString(list.getString(index));
                        return true;
                }
                return false;
        }
}

class InfoAlert extends Alert {
        public InfoAlert() {
                super("通知");
                setType(AlertType.INFO);
                setTimeout(1000);
                setString("没有记录");
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leeshuqing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值