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

📄 myaddressbook.java

📁 J2EE的无线手机通讯运用软件
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.rms.* ;
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("电话","",10,TextField.NUMERIC);

    public MyAddressBook() 
    {
        display = Display.getDisplay(this);	
    }
    public void startApp() 
    {
	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 )
	{
		//启动失败停止MIDlet
                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 = "AddForm" ;
	display.setCurrent(addForm) ;
    }

    public void commandAction(Command c,Displayable s)    
    {	   
	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 )
	{
		//启动失败停止MIDlet
                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 )
	{
		//启动失败停止MIDlet
                notifyDestroyed();
	}else
	{
		try
		{
			rs.addRecord(tmp,0,tmp.length) ;
			rs.closeRecordStore() ;
		}catch(Exception e)
		{
		}
	}
    }    

    public void pauseApp() 
    {
    }  
    public void destroyApp(boolean unconditional) 
    {
    }
    
    public RecordStore openRSAnyway(String rsname)
    {
	RecordStore rs = null ;
	//名称大于32个字符就不接受
	if(rsname.length() > 32)
		return null ;
	
	try
	{
		rs = RecordStore.openRecordStore(rsname,true) ;
		return rs ;
	}catch(Exception e)
	{
		return null ;
	}

    }
}

class FriendData
{
	String name ;
	String tel ;
	public FriendData()
	{
		name = "NO NAME" ;
		name = "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)
		{
		}
		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)
		{
		}
	}
}

⌨️ 快捷键说明

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