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

📄 hshibin143.java

📁 一个J2ME电话本程序,供您参考
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;


public class hshibin143 extends MIDlet implements CommandListener
{
	private Display display = null;  //声明Display变
	Command search = null;           //声明查询按钮
	Command quit = null;             //声明关闭按钮
	Command delete = null;           //声明删除按钮
	Command addnow = null;           //声明增加按钮
	Command edit = null;             //声明修改按钮
	Command memory = null;
	Command mainmenu = null;         //声明主菜单按钮
	Command fanhui = null;
	Command Exit = null;             
	Command phonebook=null;
	Command maindir=null;
	Command Choose_item = null;
	Command back = null;
	List menu = null;
	List menu1 = null;                //声明列表菜单
	Form ui_form = null;             //声明Form容器
	StringItem si = null;            //条目字符串
	TextField name = null;           //声明输入名字的textfield
	TextField phone = null;          //声明输入电话号码的textfield
	TextField qq = null;          //声明输入电子邮箱的textfield
	TextField nameForEdit = null;    //声明需要修改的名字的textfield
	Ticker aticker = null;
	int i,n;
	
	RecordStore recordStore = null;  //声明记录存储
	String RECORDSTORE_NAME="PHONE_DB"; 
	
	public hshibin143()       //构造函数,执行必要的初始化的工作
	{
		//用户界面初始化
		display = Display.getDisplay(this);
		quit = new Command("关闭",Command.EXIT,3);
		search = new Command("查询",Command.EXIT,2);
		delete = new Command("删除",Command.EXIT,2);
		addnow = new Command("增加",Command.EXIT,2);
		maindir=new Command("主菜单",Command.EXIT,2);
		edit = new Command("修改",Command.EXIT,2);
		memory = new Command("存储",Command.EXIT,2);
		mainmenu = new Command("返回屏幕",Command.OK,2);
		fanhui =new Command("返回",Command.OK,2);
		phonebook=new Command("电话本",Command.OK,2);
		Exit = new Command("退出",Command.EXIT,2);
		Choose_item = new Command("详细操作",Command.OK,2);
		back = new Command("返回",Command.OK,2);
		//记录存储初始化
		try{
			recordStore = RecordStore.openRecordStore("address",true);
		}catch(RecordStoreException rse){
			rse.printStackTrace();
		}
	}
		//startApp 设置菜单选项及其监听
	public void startApp(){
		menu = new List("电话簿...",List.IMPLICIT);
		menu.addCommand(maindir);
		menu.addCommand(phonebook);
		menu.setTicker(aticker);			
		menu.setCommandListener(this);
		
		display.setCurrent(menu);
	}
	//查询用户界面
	void searchScreen()
	{
		ui_form = new Form("电话簿查询");
		name = new TextField("查询姓名","",50,0);
		ui_form.append(name);//显示name在屏幕上。
		
		ui_form.addCommand(search);
		ui_form.addCommand(fanhui);	
		ui_form.setCommandListener(this);		
		display.setCurrent(ui_form);
		
	}
	//增加记录的用户界面
	void addScreen()
	{
		ui_form = new Form("电话本添加");
		name = new TextField("姓名","",50,0);
		phone = new TextField("电话号码","",50,0);
		qq = new TextField("QQ","",50,0);
		ui_form.append(name);
		ui_form.append(phone);
		ui_form.append(qq);
		ui_form.addCommand(addnow);
		ui_form.addCommand(fanhui);
		ui_form.setCommandListener(this);
		display.setCurrent(ui_form);
	}
	//修改记录的用户界面
	void editScreen()
	{
		ui_form = new Form("修改");
		nameForEdit = new TextField("需要修改的姓名","",50,0);
		name = new TextField("姓名","",50,0);
		phone = new TextField("电话号码","",50,0);
		qq = new TextField("QQ","",50,0);
		ui_form.append(nameForEdit);
		ui_form.append(name);
		ui_form.append(phone);
		ui_form.append(qq);
		ui_form.addCommand(edit);
		ui_form.addCommand(fanhui);
		ui_form.setCommandListener(this);
		display.setCurrent(ui_form);
	}
	//删除一条记录的用户界面
	void deleteScreen()
	{
		ui_form = new Form("删除");
		name = new TextField("姓名","",50,0);
		ui_form.append(name);
		ui_form.addCommand(delete);
		ui_form.addCommand(fanhui);
		ui_form.setCommandListener(this);
		display.setCurrent(ui_form);
	}
	//内存状态查询界面
	void memoryScreen()
	{
		ui_form = new Form("存储");
		address_mem();
		ui_form.addCommand(fanhui);
		ui_form.addCommand(quit);
		ui_form.setCommandListener(this);
		display.setCurrent(ui_form);

	}
	
    void detailscreen()
    {
    	i = 4;
    	menu = new List("详细操作", List.IMPLICIT);
    	menu.append("1.修改",null);
    	menu.append("2.删除",null);
    	menu.addCommand(Exit);
		menu.addCommand(back);
		menu.setCommandListener(this);
		display.setCurrent(menu);
    } 

	void phonescreen()
	{
		i = 0;
		menu = new List("电话簿程序",List.IMPLICIT);
		menu.append("1.查询", null);//append向List追加一个元素
		menu.append("2.增加", null);
		menu.append("3.存储", null);
		menu.append("4.浏览", null);
		menu.append("5.关闭", null);
		menu.addCommand(Exit);
		menu.addCommand(mainmenu);
		menu.setCommandListener(this);
		display.setCurrent(menu);
	}
	
		void maindirscreen()
	{
		i = 1;
		menu1 = new List("主目录",List.IMPLICIT);
		menu1.append("通话记录", null);//append向List追加一个元素
		menu1.append("短消息", null);
		menu1.append("铃声设置", null);
		menu1.append("系统设置", null);
		menu1.append("照相机", null);
		menu1.append("管理目录",null);
		menu1.append("娱乐功能",null);
		menu1.addCommand(Exit);
		menu1.addCommand(mainmenu);	
		menu1.setCommandListener(this);
		display.setCurrent(menu1);
	
	}

void listScreen()
{
	i = 3;
	String temp="";
	String phone_number;
	String person_name;

	menu =new List ("Address List...",List.IMPLICIT);

	try
	{
		RecordEnumeration re=recordStore.enumerateRecords(null,null,false);
		while(re.hasNextElement())
		{
			String name1=new String(re.nextRecord());
			try{
					person_name=name1.substring(0,name1.indexOf("?"));
				}
			catch (Exception ef)
			{ 
				person_name="";
			}
			try
			{
				phone_number=name1.substring(name1.indexOf("?")+1);
			}
			catch (Exception e)
			{
				phone_number="";
			}
		
	       	temp = "姓名:          "+person_name+"\n"+"电话号码:"+phone_number;
			menu.append(temp,null);
		}
	}
	catch(RecordStoreNotOpenException rsnoe)
	{
		rsnoe.printStackTrace();
	}
	catch (InvalidRecordIDException iride)
	{
		iride.printStackTrace();
	}
	catch (RecordStoreException rse)
	{
		rse.printStackTrace();
	}
	
	
   
	menu.addCommand(Choose_item);
	menu.addCommand(Exit);
	
	menu.setCommandListener(this);
	display.setCurrent(menu);

}  

	//pauseApp重置menu
	public void pauseApp()
	{
		menu = null;
		menu1 = null;
	}
	//destory关闭应用程序
	public void destroyApp(boolean unconditional) 
	{
		menu = null;
		menu1 = null;
		notifyDestroyed();
	}
	
	
	
	
	//事件处理
	public void commandAction(Command c, Displayable d){
		if(c==quit){//按下关闭按钮
			try{
				close();//关闭记录存储
			}catch(RecordStoreException rse){
				rse.printStackTrace();//销毁应用程序,触发相应事件处理
			}
			destroyApp(true);
		}
		else if(c == fanhui)
		{
			phonescreen();
		}
		else if(c == search){//按下查询按钮,触发相应事件
			
			String temp_search = name.getString();//获取要查询的姓名
			address_search(temp_search);//调用查询方法
			
		}
		else if(c==phonebook)
		{
			phonescreen();
		
		}
		else if(c==Choose_item)
		{
			detailscreen();
		}
		else if(c==back)
		{
			listScreen();
		}
		else if(c==maindir)
		{
			maindirscreen();
		}
		else if(c == edit){//按下修改按钮,触发相应事件
			String temp_nameForEdit = nameForEdit.getString();
			String temp_name = name.getString();
			String temp_phone = phone.getString();//调用修改方法
			String temp_qq = qq.getString();
			
			address_edit(temp_nameForEdit,temp_name,temp_phone,temp_qq);
		}
		else if(c == delete){//按下删除按钮,触发相应事件
			String temp_delete = name.getString();
			address_del(temp_delete);//调用删除方法
		}
		else if(c == memory){
			
			memoryScreen();
		}
		else if(c == mainmenu){//按下主菜单,触发相应事件
			startApp();//重置
		}
		else if(c == addnow){
			String temp_name = name.getString();
			String temp_phone = phone.getString();
			String temp_qq = qq.getString();
			
			address_add(temp_name,temp_phone,temp_qq);
		}
		else if(c == Exit)
		{ 
			destroyApp(false);//******************************
			notifyDestroyed();
		}
		else{
			List select =(List)display.getCurrent();
			if(i == 0)
			{	
				switch(select.getSelectedIndex())//该方法用于获得列表中被选择元素的索引号
				{
					case 0:searchScreen();
					break;
					case 1:addScreen();
					break;
					case 2:memoryScreen();
					break;
					case 3:listScreen();
					break;
					case 4:destroyApp(true);
					break;
				}

			}
			if(i == 4)
			{   
				 switch(select.getSelectedIndex())//该方法用于获得列表中被选择元素的索引号
				 {
					case 0:editScreen();
					break;
					case 1:deleteScreen();
					break;
				 }
			}
		}
	}
//查询方法

	void address_search(String address)
	{
		String temp ="";
		String phone_number="";
		String person_name;
		String check_name;
		String qq="";
		String name2;
		int add;
		int size = address.length();
		try{
			recordStore = RecordStore.openRecordStore(RECORDSTORE_NAME, true);//用于建立或者打开一个与指定MIDlet套件相联系的记录存储
			RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
			ui_form = new Form("查询结果");
			
			while(re.hasNextElement()){
				String name1 = new String (re.nextRecord());
				try{
					person_name = name1.substring(0,name1.indexOf("?"));
					add=name1.indexOf("?");
					name2=name1.substring(add+1);
					
					phone_number=name2.substring(0,name2.indexOf("?"));
					
					qq=name2.substring(name2.indexOf("?")+1);
					
					
				}catch(Exception ef){
					person_name = "请检查姓名是否正确";
					ef.printStackTrace();
				}
				if(person_name.length()>=size){
					check_name = person_name.substring(0,size);
					if(check_name.equals(address)){
						try{
							temp = temp+"姓名:          "+person_name+'\n'+"电话号码:"+phone_number+'\n'+"QQ:            "+qq+'\n'+'\n';
							
						}catch(Exception e){
							temp="123";
						}
					}
				}
				if(temp.equals("123")){
					temp = "没有找到";
				}
				
			}

		}catch(RecordStoreNotOpenException rsnoe){
			rsnoe.printStackTrace();
		}catch(InvalidRecordIDException irid){
			irid.printStackTrace();
		}catch(RecordStoreException rse){
			rse.printStackTrace();
		}
		ui_form.append(temp);
		
		ui_form.addCommand(quit);
		ui_form.addCommand(fanhui);
		
		ui_form.setCommandListener(this);
		display.setCurrent(ui_form);
}
//删除方法
	void address_del(String address)
	{
		String temp = " ";
		String person_name;
		int id;
		int del_id = 0;
		try
		{
			recordStore = RecordStore.openRecordStore(RECORDSTORE_NAME, true);//记录集合
			RecordEnumeration re = recordStore.enumerateRecords(null,null,false);
			ui_form = new Form("删除结果");
			
			while(re.hasNextElement())
			{
				id = re.nextRecordId();
				String name1 = new String(recordStore.getRecord(id));
				try{
					person_name = name1.substring(0,name1.indexOf("?"));
				}catch(Exception ef){
					person_name = "请检查姓名名是否正确";
				}
			
				if(person_name.equals(address)){
					del_id = id;
				}
			}
			if(del_id!=0){
				recordStore.deleteRecord(del_id);
				temp = "成功删除一条记录";
			}
			else{
				temp = "所指定的记录不在电话簿内";
			}
		}catch(Exception e){
			
		}
		ui_form.append(temp);
		ui_form.addCommand(quit);
		ui_form.addCommand(fanhui);
		ui_form.setCommandListener(this);
		display.setCurrent(ui_form);
	}
	//内存查询方法
	void address_mem(){
		int num=0;
		String temp="";
		try{
			
			recordStore = RecordStore.openRecordStore(RECORDSTORE_NAME, true);
			
			ui_form = new Form("内存状态");
			

				num=recordStore.getNumRecords();
				temp = "总共有"+num+"条记录";
			
			
		}catch(RecordStoreNotOpenException rsnoe){
			rsnoe.printStackTrace();
		}catch(RecordStoreFullException rsfe){
			rsfe.printStackTrace();
		}catch(RecordStoreNotFoundException rsnfe){
			rsnfe.printStackTrace();
		}catch(RecordStoreException rse){
			rse.printStackTrace();
		}
		
		ui_form.append(temp);
		
		
		ui_form.addCommand(quit);
		ui_form.addCommand(fanhui);
		
		ui_form.setCommandListener(this);
		display.setCurrent(ui_form);
	}
//增加方法
	void address_add(String address,String phone,String qq){
		String data = address+"?"+phone+"?"+qq;
		System.out.println(data);
		try{
			recordStore = RecordStore.openRecordStore(RECORDSTORE_NAME, true);
			
			byte[] b=data.getBytes();
			recordStore.addRecord(b, 0, b.length);
			

		}catch(RecordStoreException rse){
			rse.printStackTrace();
		}
		ui_form = new Form("添加成功");
		ui_form.append("成功添加一条记录");
		
		ui_form.addCommand(quit);
		ui_form.addCommand(fanhui);
		
		ui_form.setCommandListener(this);
		display.setCurrent(ui_form);
	}
	//修改方法
	void address_edit(String addressForEdit,String address,String phone,String qq){
		int id=1;
		int edit_id=0;
		String person_name;
		String temp = "error";
		try{
			recordStore = RecordStore.openRecordStore(RECORDSTORE_NAME, true);
			RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
			ui_form = new Form("修改");
			while(re.hasNextElement()){
				id = re.nextRecordId();
				String name1 = new String(recordStore.getRecord(id));
				try{
					person_name = name1.substring(0,name1.indexOf("?"));
				}catch(Exception ef){
					person_name = "请检查姓名是否正确";
				}
				if(person_name.equals(addressForEdit)){
					edit_id=id;
				}
			}
			if(edit_id!=0){
				String data = address+"?"+phone+"?"+qq;
				byte[] b_data = data.getBytes();
				recordStore.setRecord(edit_id, b_data,0, b_data.length);
				temp = "成功修改一条记录";
			}
			else{
				temp = "要修改的记录不在电话簿内";
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		ui_form.append(temp);
		
		ui_form.addCommand(quit);
		ui_form.addCommand(fanhui);
		
		ui_form.setCommandListener(this);
		
		display.setCurrent(ui_form);
	}
	//关闭记录存储
	public void close()throws RecordStoreNotOpenException,RecordStoreException,
	RecordStoreFullException
	{
		recordStore.closeRecordStore();
	}
			
}

⌨️ 快捷键说明

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