import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.lcdui.*; public class Memo extends MIDlet implements CommandListener{ private Display display; private String[] stringArray; private int tbLen,numEdit,numDelete; private TextBox tbAdd,tbView,tbEdit; private List list; private Alert al; private Command saveCommand=new Command("Save",Command.OK,1); private Command addCommand=new Command("Add",Command.ITEM,2); private Command editCommand=new Command("Edit",Command.ITEM,3); private Command deleteCommand=new Command("Delete",Command.ITEM,4); private Command viewCommand=new Command("View",Command.ITEM,1); private Command exitCommand=new Command("Exit",Command.EXIT,5); private Command okCommand=new Command("OK",Command.OK,1); private Command cancelCommand=new Command("Cancel",Command.CANCEL,1);
public Memo() { display=Display.getDisplay(this); list=new List("Killua's Memo",Choice.IMPLICIT); stringArray=new String[100]; tbAdd=new TextBox("Add","",50,TextField.ANY); tbEdit=new TextBox("Edit","",50,TextField.ANY); tbView=new TextBox("View","",50,TextField.UNEDITABLE); al=new Alert("Waring"); al.setType(AlertType.WARNING); al.setString("you delete the record,are you sure?"); al.setTimeout(Alert.FOREVER); tbLen=numEdit=0; } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { tbAdd.addCommand(saveCommand); tbAdd.addCommand(cancelCommand);
tbEdit.addCommand(saveCommand); tbEdit.addCommand(cancelCommand);
tbView.addCommand(cancelCommand);
list.addCommand(addCommand); list.addCommand(editCommand); list.addCommand(deleteCommand); list.addCommand(viewCommand); list.addCommand(exitCommand);
al.addCommand(okCommand); al.addCommand(cancelCommand);
al.setCommandListener(this); tbAdd.setCommandListener(this); tbEdit.setCommandListener(this); tbView.setCommandListener(this); list.setCommandListener(this);
display.setCurrent(tbAdd); } public void commandAction(Command cmd,Displayable d) { if(tbAdd.isShown()) { if(cmd==saveCommand) { String stringAdd=tbAdd.getString(); if(!stringAdd.equals("")) { stringArray[tbLen++]=stringAdd; list.append(stringAdd,null); } display.setCurrent(list); } } if(tbEdit.isShown()) { if(cmd==saveCommand) { String stringEdit=tbEdit.getString(); stringArray[numEdit]=stringEdit; list.set(numEdit,stringEdit, null); display.setCurrent(list); } } if(al.isShown()) { if(cmd==okCommand) { list.delete(numDelete); for(int j=numDelete;j<tbLen-1;j++) stringArray[j]=stringArray[j+1]; tbLen--; display.setCurrent(list); } } if(cmd==cancelCommand) display.setCurrent(list); if(list.isShown()) { if(cmd==addCommand) { tbAdd.setString(""); display.setCurrent(tbAdd); } if(cmd==editCommand) { numEdit=list.getSelectedIndex(); tbEdit.setString(stringArray[numEdit]); display.setCurrent(tbEdit); } if(cmd==deleteCommand) { display.setCurrent(al); numDelete=list.getSelectedIndex(); } if(cmd==viewCommand) { int i=list.getSelectedIndex(); tbView.setString(stringArray[i]); display.setCurrent(tbView); } } if(cmd==exitCommand) { notifyDestroyed(); } } }
|