J2ME作业,手机用户管理系统

又是个作业。。当时搞到两天多。。有个问题一直没解决。。得YOYO大牛指点。顿悟。。

 

定义一个User类,保存用户字段。

  1. import java.util.Date;
  2. public class User {
  3.     private String name=new String();
  4.     private int age=0;
  5.     private boolean sex=true;
  6.     private Date birthday=new Date();
  7.     private String nativePlace=new String();
  8.     private String postcode=new String();
  9.     private String address=new String();
  10.     User()
  11.     {
  12.     }
  13.     public void setName(String s)
  14.     {
  15.         name=s;
  16.     }
  17.     public void setAge(int x)
  18.     {
  19.         age=x;
  20.     }
  21.     public void setSex(boolean s)
  22.     {
  23.         sex=s;
  24.     }
  25.     public void setBirthday(Date d)
  26.     {
  27.         birthday=d;
  28.     }
  29.     public void setNativePlace(String s)
  30.     {
  31.         nativePlace=s;
  32.     }
  33.     public void setPostcode(String s)
  34.     {
  35.         postcode=s;
  36.     }
  37.     public void setAddress(String s)
  38.     {
  39.         address=s;
  40.     }
  41.     public String getName()
  42.     {
  43.         return name;
  44.     }
  45.     public int getAge()
  46.     {
  47.         return age;
  48.     }
  49.     public boolean getSex()
  50.     {
  51.         return sex;
  52.     }
  53.     public Date getBirthday()
  54.     {
  55.         return birthday;
  56.     }
  57.     public String getNativePlace()
  58.     {
  59.         return nativePlace;
  60.     }
  61.     public String getPostcode()
  62.     {
  63.         return postcode;
  64.     }
  65.     public String getAddress()
  66.     {
  67.         return address;
  68.     }
  69. }

 

AlertThread类,用来实现开机进度条功能(由于没学过Thread所以写的很是不完善)

  1. import javax.microedition.lcdui.Alert;
  2. import javax.microedition.lcdui.Gauge;
  3. public class AlertThread extends Thread {
  4.     Alert al;
  5.     public AlertThread(Alert al) {
  6.         this.al=al;
  7.     }
  8.     public void run()
  9.     {
  10.         Gauge indicator=al.getIndicator();
  11.         for(int i=0;i<10;i++)
  12.         {
  13.             indicator.setValue(i);
  14.             try
  15.             {
  16.                 Thread.sleep(500);
  17.             }
  18.             catch(Exception e)
  19.             {
  20.                 
  21.             }
  22.         }
  23.     }
  24. }

 

这个当然是主程序了,O(∩_∩)O

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

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值