⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maintainlist.java

📁 用Eclipse开发J2me书籍的程序源码。
💻 JAVA
字号:
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.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MaintainList extends MIDlet  implements CommandListener {
    private Display display;
    
    //定义List菜单的内容
    private static String menuItems[] = {"List Item1",
            "List Item2",
            "List Item3",
            "List Item4",
            "List Item5"
    };

    private List lstMultiple = new List(
            "Multiple List",                    //标题
            List.MULTIPLE,                      //多选类型 
            menuItems,                          //设置列表内容   
            null);                              //列表不使用图标
    
    //用于末尾添加选项
    private Command cmdAppend = new Command("Append", Command.SCREEN, 1);
    //在当前位置插入选项
    private Command cmdInsert = new Command("Insert", Command.SCREEN, 1);
    //全部删除
    private Command cmdDeleteAll = new Command("Delete All", 
            Command.SCREEN, 1);
    //删除选中的选项
    private Command cmdDelete = new Command("Delete", 
            Command.SCREEN, 1);
    private Command cmdSet = new Command("Set", 
            Command.SCREEN, 1);

    //Exit
    private Command cmdExit = new Command("Exit", Command.EXIT, 1);
    
    public MaintainList() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void startApp() throws MIDletStateChangeException {
        //设置命令监听
        lstMultiple.addCommand(cmdExit);
        lstMultiple.addCommand(cmdAppend);
        lstMultiple.addCommand(cmdInsert);
        lstMultiple.addCommand(cmdDeleteAll);   
        lstMultiple.addCommand(cmdDelete);
        lstMultiple.addCommand(cmdSet);
        lstMultiple.setCommandListener(this);
        
        //获得当前MIDlet的Display对象
        display = Display.getDisplay(this); 
        //设置List对象为当前显示对象
        display.setCurrent(lstMultiple);  
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void destroyApp(boolean arg0) 
        throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command c, Displayable d) {
        if (c == this.cmdAppend) {
            //在末尾添加选项
            lstMultiple.append("List Item" + 
                    String.valueOf(lstMultiple.size()+1), null);
        } else if (c == this.cmdInsert) {
            //在当前位置插入选项
            for (int i=0; i< lstMultiple.size(); i++) {
                //取第一个被选中的
                if (lstMultiple.isSelected(i)) {
                    lstMultiple.insert(i, 
                            "List Item" + String.valueOf(lstMultiple.size()+1), 
                            null);
                    
                    break;
                }
            }
        } else if (c == this.cmdDeleteAll) {
            //全部删除
            lstMultiple.deleteAll();
        } else if (c == this.cmdDelete) {
            //删除选中的选项                
            //for (int i=0; i< lstMultiple.size(); i++) {
            for (int i=lstMultiple.size()-1; i>=0; i--) {
                if (lstMultiple.isSelected(i)) {                    
                    lstMultiple.delete(i);
                }
            }           
        } else if (c == this.cmdSet) {
            //更新选中的选项
            for (int i=0; i< lstMultiple.size(); i++) {
                if (lstMultiple.isSelected(i)) {                    
                    lstMultiple.set(i, lstMultiple.getString(i)+
                            " is updated", null);
                }
            }           
        } else if (c == this.cmdExit) {
            notifyDestroyed();
        }
    } 
}

⌨️ 快捷键说明

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