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

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

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

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
import java.util.Date;
public class User {
private String name=new String();
private int age=0;
private boolean sex=true;
private Date birthday=new Date();
private String nativePlace=new String();
private String postcode=new String();
private String address=new String();
User()
{
}
public void setName(String s)
{
name=s;
}
public void setAge(int x)
{
age=x;
}
public void setSex(boolean s)
{
sex=s;
}
public void setBirthday(Date d)
{
birthday=d;
}
public void setNativePlace(String s)
{
nativePlace=s;
}
public void setPostcode(String s)
{
postcode=s;
}
public void setAddress(String s)
{
address=s;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public boolean getSex()
{
return sex;
}
public Date getBirthday()
{
return birthday;
}
public String getNativePlace()
{
return nativePlace;
}
public String getPostcode()
{
return postcode;
}
public String getAddress()
{
return address;
}
}

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

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

}
}
}
}

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

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);
//创建Command
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);
//添加Command
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)
{
//destroyApp(false);
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);
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!