1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| import java.util.Date; import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class UserManage extends MIDlet implements CommandListener{ private User user[]=new User[100]; private int userLen=0,sel=0; private Display display; private Form userForm; private List userList; private TextField tfName,tfAge,tfNativePlace,tfPostcode,tfAddress; private Command saveCommand,cancelCommand,editCommand,addCommand,deleteCommand,exitCommand,enterCommand; private ChoiceGroup cgSex; private DateField dfBirthday; private boolean isEdit=false;
public UserManage() { userForm=new Form("用户信息"); userList=new List("用户列表",List.IMPLICIT); tfName=new TextField("姓名","",50,TextField.ANY); tfAge=new TextField("年龄","",5,TextField.NUMERIC); tfNativePlace=new TextField("籍贯","",20,TextField.ANY);; tfPostcode=new TextField("邮编","",5,TextField.NUMERIC); tfAddress=new TextField("地址","",100,TextField.ANY); String[] sexArray=new String[]{"男","女"}; cgSex=new ChoiceGroup("性别",ChoiceGroup.POPUP,sexArray,null); dfBirthday=new DateField("出生年月",DateField.DATE); saveCommand=new Command("保存",Command.OK,1); cancelCommand=new Command("取消",Command.CANCEL,1); addCommand=new Command("添加",Command.ITEM,1); editCommand=new Command("编辑",Command.ITEM,2); deleteCommand=new Command("删除",Command.ITEM,3); exitCommand=new Command("退出",Command.EXIT,1); enterCommand=new Command("进入",Command.OK,1); userForm.append(tfName); userForm.append(tfAge); userForm.append(cgSex); userForm.append(dfBirthday); userForm.append(tfNativePlace); userForm.append(tfPostcode); userForm.append(tfAddress); userForm.addCommand(saveCommand); userForm.addCommand(cancelCommand); userForm.setCommandListener(this); userList.addCommand(addCommand); userList.addCommand(deleteCommand); userList.addCommand(editCommand); userList.addCommand(exitCommand); userList.setCommandListener(this);
} protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { display=Display.getDisplay(this); Alert al=new Alert(""); al.setType(AlertType.INFO); al.setTimeout(Alert.FOREVER); Gauge g=new Gauge(null,false,5,0); g.getPreferredHeight(); g.getPreferredWidth(); al.setIndicator(g); al.setString("用户管理系统载入中...."); al.addCommand(enterCommand); al.addCommand(exitCommand); al.setCommandListener(this); AlertThread t=new AlertThread(al); t.start(); display.setCurrent(al); } public void commandAction(Command c,Displayable d) { if(userForm.isShown()) { if(c==saveCommand) { user[userLen] = new User(); user[userLen].setName(tfName.getString()); user[userLen].setAge(Integer.parseInt(tfAge.getString())); user[userLen].setSex(!cgSex.isSelected(0)); user[userLen].setBirthday(dfBirthday.getDate()); user[userLen].setNativePlace(tfNativePlace.getString()); user[userLen].setPostcode(tfPostcode.getString()); user[userLen].setAddress(tfAddress.getString()); userList.append(user[userLen].getName(),null); userLen++; if(isEdit) { delete(sel); } } display.setCurrent(userList); } if(userList.isShown()) { sel=userList.getSelectedIndex(); if(c==addCommand) { tfName.setString(""); tfAge.setString(""); tfNativePlace.setString(""); tfPostcode.setString(""); tfAddress.setString(""); cgSex.setSelectedIndex(0, true); dfBirthday.setDate(new Date()); isEdit=false; display.setCurrent(userForm); } if(c==editCommand) { tfName.setString(user[sel].getName()); tfAge.setString(String.valueOf(user[sel].getAge())); tfNativePlace.setString(user[sel].getNativePlace()); tfPostcode.setString(user[sel].getPostcode()); tfAddress.setString(user[sel].getAddress()); if(user[sel].getSex()) { cgSex.setSelectedIndex(0,true); } else { cgSex.setSelectedIndex(1,true); } dfBirthday.setDate(user[sel].getBirthday()); isEdit=true; display.setCurrent(userForm); } if(c==deleteCommand) { delete(sel); }
} if(c==cancelCommand) { display.setCurrent(userList); } if(c==exitCommand) { notifyDestroyed(); } if(c==enterCommand) { display.setCurrent(userForm); } } public void delete(int n) {
for(int i=n;i<userLen-1;i++) { user[i]=user[i+1]; } userLen--; userList.delete(n); } }
|