📄 myaddressbook.java
字号:
/*
* 创建日期 2005-9-23
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package src;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.*;
import java.io.*;
/**
* @author 钟宁林
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class MyAddressBook extends MIDlet implements CommandListener {
/**
*
*/
private Display display;
String dbname = "MyAddressBook";
String current = "";
Form addForm = new Form("添加资料");
TextField tf1 = new TextField("姓名","",20,TextField.ANY);
TextField tf2 = new TextField("电话","",15,TextField.NUMERIC);
public MyAddressBook() {
super();
// TODO 自动生成构造函数存根
display = Display.getDisplay(this);
}
/* (非 Javadoc)
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
Command add = new Command("确定",Command.SCREEN,1);
Command back = new Command("返回上页",Command.SCREEN,2);
addForm.append(tf1);
addForm.append(tf2);
addForm.addCommand(add);
addForm.addCommand(back);
addForm.setCommandListener(this);
MainForm();
}
public void MainForm()
{
List l = new List("我的通讯簿",Choice.IMPLICIT);
l.append("通讯簿列表",null);
l.append("添加资料",null);
l.setCommandListener(this);
current = "MainForm";
display.setCurrent(l);
}
public void ListAllForm()
{
List l = new List("通讯簿列表",Choice.EXCLUSIVE);
Command back = new Command ("回上页",Command.SCREEN,2);
l.addCommand(back);
l.setCommandListener(this);
RecordStore rs = openRSAnyway(dbname);
if(rs == null)
{
notifyDestroyed();
}
else
{
try
{
RecordEnumeration re = rs.enumerateRecords(null,null,false);
FriendData data = new FriendData();
if(re.numRecords() == 0)
{
l.append("没有任何资料",null);
current = "ListAllForm";
display.setCurrent(l);
return ;
}
while(re.hasNextElement())
{
byte tmp[] =re.nextRecord();
data.decode(tmp);
l.append(data.name,null);
}
rs.closeRecordStore();
}catch(Exception e){
}
}
Command info = new Command("详细",Command.SCREEN,1);
l.addCommand(info);
current = "ListAllForm";
display.setCurrent(l);
}
public void AddForm()
{
current = "AddFrom";
display.setCurrent(addForm);
}
/* (非 Javadoc)
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp() {
// TODO 自动生成方法存根
}
/* (非 Javadoc)
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO 自动生成方法存根
}
/* (非 Javadoc)
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command c, Displayable s)
{
// TODO 自动生成方法存根
if(c == List.SELECT_COMMAND && current.equals("MainForm"))
{
List tmp = (List) s;
switch(tmp.getSelectedIndex())
{
case 0:
ListAllForm();
break;
case 1:
AddForm();
break;
}
}
if(c.getLabel().equals("回上页"))
{
MainForm();
}
if(c.getLabel().equals("确定"))
{
addData();
ListAllForm();
}
if(c.getLabel().equals("详细"))
{
List tmp = (List) s ;
searchData(tmp.getString(tmp.getSelectedIndex()));
}
}
public void searchData(String name)
{
Form f = new Form("详细");
Command back = new Command("回上页",Command.SCREEN,1);
f.addCommand(back);
f.setCommandListener(this);
RecordStore rs = openRSAnyway(dbname);
if(rs == null)
{
notifyDestroyed();
}else
{
try
{
RecordEnumeration re = rs.enumerateRecords(null,null,false);
FriendData data = new FriendData();
while(re.hasNextElement())
{
byte tmp[] = re.nextRecord();
data.decode(tmp);
if(data.name.equals(name))
{
f.append("姓名:\n");
f.append(data.name+"\n");
f.append("电话:\n");
f.append(data.tel+"\n");
display.setCurrent(f);
return ;
}
}
rs.closeRecordStore();
}
catch(Exception e)
{
}
}
}
public void addData()
{
FriendData data = new FriendData();
data.name = tf1.getString();
data.tel = tf2.getString();
byte []tmp = data.encode();
RecordStore rs = openRSAnyway(dbname);
if(rs == null)
{
notifyDestroyed();
}
else
{
try
{
rs.addRecord(tmp,0,tmp.length);
rs.closeRecordStore();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public RecordStore openRSAnyway(String rsname)
{
RecordStore rs = null;
if(rsname.length() > 37 )
return null;
try
{
rs = RecordStore.openRecordStore(rsname,true);
return rs;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
class FriendData
{
String name ;
String tel ;
public FriendData()
{
name = "NO NAME" ;
tel = "NO TEL";
}
public byte[] encode()
{
byte [] result = null;
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(name);
dos.writeUTF(tel);
result = bos.toByteArray();
dos.close();
bos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return result;
}
public void decode(byte[] data)
{
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bis);
name = dis.readUTF();
tel = dis.readUTF();
dis.close();
bis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -