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); } }
|