⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 telbook.java

📁 j2me下的电话本 使用环境为Eclipse
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.*;
import java.io.*;


public class TelBook extends MIDlet implements CommandListener {
	

	public TelBook() {
		// TODO Auto-generated constructor stub
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		//主页面生成机制	
		display = Display.getDisplay(this);
		Main = new List("电话本系统V1.0",List.IMPLICIT);
		CO_New = new Command("新增联系人",Command.SCREEN,1);
		CO_Mend = new Command("修改联系人",Command.SCREEN,1);
		CO_Del = new Command("删除联系人",Command.SCREEN,1);
		CO_Exit = new Command("退出",Command.EXIT,0);
		Main.addCommand(CO_New);
		Main.addCommand(CO_Mend);
		Main.addCommand(CO_Del);
		Main.addCommand(CO_Exit);
		display.setCurrent(Main);
		//初始化电话本主界面联系人资料
		Search();
		Main.setCommandListener(this);
		//新增联系人界面机制
		NewForm = new Form("修改联系人");
		TE_Name = new TextField("联系人姓名","",100,TextField.ANY);
		TE_Tel = new TextField("联系人号码","",20,TextField.NUMERIC);
		NewForm.append(TE_Name);
		NewForm.append(TE_Tel);
		NE_Back = new Command("后退",Command.BACK,1);
		NE_OK   = new Command("确定",Command.OK,1);
		NewForm.addCommand(NE_Back);
		NewForm.addCommand(NE_OK);
		//修改联系人机制
		MendForm = new Form("新增联系人");
		ME_Name = new TextField("联系人姓名","",100,TextField.ANY);
		ME_Tel = new TextField("联系人号码","",20,TextField.NUMERIC);
		MendForm.append(ME_Name);
		MendForm.append(ME_Tel);
		ME_Back = new Command("后退",Command.BACK,1);
		ME_OK   = new Command("确定",Command.OK,1);
		MendForm.addCommand(ME_Back);
		MendForm.addCommand(ME_OK);		
		//系统信息机制
		SysAL = new Alert("系统提示","",null,null);
		SysAL.setTimeout(3000);
		//初始化RMS机制
		try{
		RE = RecordStore.openRecordStore("Tel", true);
		RE.closeRecordStore();
		}catch(Exception e){
			e.printStackTrace();
		}
     	// TODO Auto-generated method stub

	}

	public void commandAction(Command c, Displayable d) {
		
		if(c == CO_Exit)
		{
			try{
			destroyApp(false);
			}catch(MIDletStateChangeException e){		
			}
			notifyDestroyed();
		}
		if(c == CO_New)
		{
			display.setCurrent(NewForm);
			NewForm.setCommandListener(this);
			
		}
		if((c == NE_Back) ||(c == ME_Back) )
		{
			Main.deleteAll();
			Search();
			display.setCurrent(Main);
			Main.setCommandListener(this);	
		}
		if(c == NE_OK)
		{
			//判断正确输入
			if(TE_Name.getString().equals("")){
				SysAL.setString("请输入联系人姓名");
				display.setCurrent(SysAL,NewForm);
			}
			else if(TE_Tel.getString().equals("")){
				SysAL.setString("请输入联系人号码");
				display.setCurrent(SysAL,NewForm);
			}
			//调用新增联系人函数
			NewTel();
		}
		if( c == CO_Del)
		{
			//删除函数机制
			Del();
		}
		if( c == CO_Mend){
			display.setCurrent(MendForm);
			MendForm.setCommandListener(this);	
		}
		if( c== ME_OK){
			Mend();
			Main.deleteAll();
			Search();
			SysAL.setString("修改成功");		
			display.setCurrent(SysAL,Main);
			Main.setCommandListener(this);	
			
		}
		// TODO Auto-generated method stub

	}
	//初始化联系人函数
	public void NewTel(){
		try{
			RE = RecordStore.openRecordStore("Tel", false);
			}catch(Exception e){
				e.printStackTrace();
			}
		//联系人与联系号码的序号
		try{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(baos);
			dos.writeUTF(TE_Name.getString());
			dos.writeUTF("----");
			dos.writeUTF(TE_Tel.getString());
			RE.addRecord(baos.toByteArray(), 0, baos.toByteArray().length);	
			dos.close();
			baos.close();
			RE.closeRecordStore();
		}catch(RecordStoreException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		TE_Name.setString("");
		TE_Tel.setString("");
		SysAL.setString("输入成功");		
		display.setCurrent(SysAL,NewForm);
	}
	//遍历联系人函数
	public void Search()
	{
		
		String SE_Tel;
		try{
			RE = RecordStore.openRecordStore("Tel", false);
			RS = RE.enumerateRecords(null, null, true);
			}catch(Exception e){
				e.printStackTrace();
			}
			while(RS.hasNextElement()){
				try{
				//byte[] data = RS.nextRecord();
				//ByteArrayInputStream bais = new ByteArrayInputStream(data);
				//DataInputStream dis = new DataInputStream(bais);
				//SE_Tel = dis.readUTF();			    
				SE_Tel = new String(RS.nextRecord());
				Main.append(SE_Tel, null);				
				}catch(Exception e){
					e.printStackTrace();
				}			
			}
			try{
				RE.closeRecordStore();
			}catch(Exception e){
				e.printStackTrace();
			}		
	}
	
	//删除联系人机制
	public void Del()
	{
		//删除RMS中的存档
		try{
			RE = RecordStore.openRecordStore("Tel", false);
			DE1 = RE.enumerateRecords(null, null, true);
			DE2 = RE.enumerateRecords(null, null, true);
			}catch(Exception e){
				e.printStackTrace();
			}
	    //选中的项LIST
		String Del_Temp1 = Main.getString(Main.getSelectedIndex());
		//遍历的项
		String Del_Temp2;		
		//遍历项的序列	
		int Del_Temp3 ;
		while(DE1.hasNextElement()){
			try{
			Del_Temp2 = new String(DE1.nextRecord());
			Del_Temp3 = DE2.nextRecordId();
			if(Del_Temp2.equals(Del_Temp1)){
			 RE.deleteRecord(Del_Temp3);
			 break;
			}
			}catch(Exception e)
			{
			  e.printStackTrace();
			}
		}
		try{
			RE.closeRecordStore();
			}catch(Exception e){
				e.printStackTrace();
			}
		//删除List中的映象
		int DE_Index = Main.getSelectedIndex();
		Main.delete(DE_Index);		
	}
	//修改联系人机制
	public void Mend(){
		//删除RMS中的存档
		try{
			RE = RecordStore.openRecordStore("Tel", false);
			ME1 = RE.enumerateRecords(null, null, true);
			ME2 = RE.enumerateRecords(null, null, true);
			}catch(Exception e){
				e.printStackTrace();
			}
	    //选中的项LIST
		String ME_Temp1 = Main.getString(Main.getSelectedIndex());
		//遍历的项
		String ME_Temp2;		
		//遍历项的序列	
		int ME_Temp3 ;
		while(ME1.hasNextElement()){
			try{
				ME_Temp2 = new String(ME1.nextRecord());
				ME_Temp3 = ME2.nextRecordId();
			if(ME_Temp2.equals(ME_Temp1)){
		        ByteArrayOutputStream baos = new ByteArrayOutputStream();
				DataOutputStream dos = new DataOutputStream(baos);
				dos.writeUTF(ME_Name.getString());
				dos.writeUTF("----");
				dos.writeUTF(ME_Tel.getString());
			    RE.setRecord(ME_Temp3, baos.toByteArray(), 0, baos.toByteArray().length);
			    break;
			}
			}catch(Exception e)
			{
			  e.printStackTrace();
			}
		}
		try{
			RE.closeRecordStore();
			}catch(Exception e){
				e.printStackTrace();
			}
		
	}
	
	
	
	
	
	private Display display; 
	//生成主界面 用于列出电话本的信息
	private List Main;
	private Command CO_New;
	private Command CO_Mend;
	private Command CO_Del;
	private Command CO_Exit;
	//生成新增电话本界面
	private Form NewForm;
	private TextField TE_Name;
	private TextField TE_Tel;
	private Command NE_Back;
	private Command NE_OK;
	//生成RMS储蓄
	private RecordStore RE;
	private RecordEnumeration RS ;
	//系统提示界面
	private Alert SysAL;
	//修改,删除联系人接口
	RecordEnumeration DE1;
	RecordEnumeration DE2;
	RecordEnumeration ME1;
	RecordEnumeration ME2;
	//修改联系人界面
	private Form MendForm;
	private TextField ME_Name;
	private TextField ME_Tel;
	private Command ME_Back;
	private Command ME_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -