//程序入口//
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Job extends MIDlet {
public static Job instance;
private MainScreen displayable = new MainScreen();
//构造函数//
public Job() {
instance = this;
}
public void startApp() {
Display.getDisplay(this).setCurrent(displayable);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}
//主界面//
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
public class MainScreen extends List implements CommandListener {
private Command CMDview, CMDadd, CMDedit, CMDdelete;
private Alert alert;
public EditForm editForm;
private DataManager datamanager;
public Hashtable getchange, table;
public MainScreen() {
super(" 电话本", Choice.IMPLICIT);
listItem();
CMDview = new Command("查看", Command.BACK, 1);
CMDadd = new Command("添加", Command.OK, 1);
CMDedit = new Command("编辑", Command.OK, 1);
CMDdelete = new Command("删除", Command.OK, 1);
this.addCommand(CMDview);
this.addCommand(CMDadd);
this.addCommand(CMDedit);
this.addCommand(CMDdelete);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable display) {
int index = this.size();
if (c == CMDview) {
if (index <= 0) {
this.showAlertNoInfor("对不起,没有信息......");
} else {
showInfor("查看");
editForm.removeCommand(editForm.CMD_OK);
}
} else if (c == CMDadd) {
addListItem();
} else if (c == CMDedit) {
if (index <= 0) {
this.showAlertNoInfor("对不起,没有信息可以编辑");
} else { showInfor("编辑"); }
} else if (c == CMDdelete) {
if (index <= 0) {
this.showAlertNoInfor("对不起,没有信息可以被删除");
} else {
deleteListItem();
}
}
}
private void showAlertNoInfor(String ale) {
alert = new Alert(" 通知");
alert.setType(AlertType.INFO);
alert.setTimeout(1000);
alert.setString(ale);
Display.getDisplay(Job.instance).setCurrent(alert);
}
private void showInfor(String title) {
editForm = new EditForm(title);
int index = this.getSelectedIndex();
String selected = this.getString(index);
Vector value = (Vector) table.get(selected);
editForm.TFname.setString((String) value.elementAt(0));
editForm.TFmobile.setString((String) value.elementAt(1));
editForm.TFtel.setString((String) value.elementAt(2));
editForm.TFemail.setString((String) value.elementAt(3));
editForm.TFaddress.setString((String) value.elementAt(4));
}
public void listItem() {
datamanager = null;
datamanager = DataManager.getDataManage();
if (table == null) { //
table = datamanager.getStart(); //
getchange = table; //这部分代码可删除,只是为了测试方便
} else { //
table = getchange; //
} //
Enumeration enu = table.keys();
while (enu.hasMoreElements()) {
String key = (String) enu.nextElement();
Vector value = (Vector) table.get(key);
this.append((String) value.elementAt(0), null);
}
}
private void deleteListItem() {
int num = this.getSelectedIndex();
this.getchange.remove(this.getString(num));
this.delete(num);
}
private void addListItem() {
editForm = new EditForm("添加");
}
}
//编辑窗口//
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
public class EditForm extends Form implements CommandListener {
public TextField TFname, TFmobile, TFtel, TFemail, TFaddress;
public Command CMD_OK, CMD_BACK;
public MainScreen showMainScreen;
private DataManager data = null;
public EditForm(String operation) {
super(operation);
CMD_BACK = new Command("返回", Command.BACK, 1);
CMD_OK = new Command("确定", Command.OK, 1);
TFname = new TextField("姓名", "", 8, TextField.ANY);
TFmobile = new TextField("手机", "", 10, TextField.NUMERIC);
TFtel = new TextField("电话", "", 10, TextField.NUMERIC);
TFemail = new TextField("邮箱", "", 10, TextField.EMAILADDR);
TFaddress = new TextField("地址", "", 20, TextField.ANY);
this.addCommand(CMD_OK);
this.addCommand(CMD_BACK);
this.append(TFname);
this.append(TFmobile);
this.append(TFtel);
this.append(TFemail);
this.append(TFaddress);
this.setCommandListener(this);
Display.getDisplay(Job.instance).setCurrent(this);
}
public void commandAction(Command c, Displayable display) {
if (c == CMD_OK) {
addData();
showMainScreen = new MainScreen();
Display.getDisplay(Job.instance).setCurrent(showMainScreen);
} else if (c == CMD_BACK) {
showMainScreen = new MainScreen();
Display.getDisplay(Job.instance).setCurrent(showMainScreen);
}
}
private void addData() {
data = new DataManager();
data = DataManager.getDataManage();
data.vector = null;
data.vector = new Vector(10);
data.vector.addElement(TFname.getString());
data.vector.addElement(TFmobile.getString());
data.vector.addElement(TFtel.getString());
data.vector.addElement(TFemail.getString());
data.vector.addElement(TFaddress.getString());
data.table.put(TFname.getString(), data.vector);
}
}
//数据//请注意由于数据是有代码直接添加的,因此对以下两个数据的部分操作会失败,使用者可以删除该部分的代码,对于用户自己添加的数据不会有影响//
import java.util.Hashtable;
import java.util.Vector;
public class DataManager {
public static DataManager instance = null;
public Vector vector;
public Hashtable table;
public DataManager() {
vector = new Vector(10);
table = new Hashtable(10);
}
public static DataManager getDataManage() {
if (instance == null) {
instance = new DataManager();
}
return instance;
}
public Hashtable getStart() {
vector = null; //Very Important//
vector = new Vector(10);
vector.addElement("CHEN");
vector.addElement("1234567890");
vector.addElement("666666");
vector.addElement("123@qq.com");
vector.addElement("University");
table.put("CHEN", vector);
vector = null;
vector = new Vector(10);
vector.addElement("LI");
vector.addElement("12323");
vector.addElement("888888");
vector.addElement("163@qq.com");
vector.addElement("Earth");
table.put("LI", vector);
return table;
}
}
//有兴趣的可以将代码扩充完善。例如:欢迎界面,各种操作的提示信息等//