CSDN博客

img leeshuqing

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

发表于2008/9/28 22:52:00  259人阅读

分类: J2ME

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("没有记录");
        }
}

阅读全文
0 0

相关文章推荐

img
取 消
img