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

📄 addressbook.java

📁 jBuilderX无线应用开发源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.* ;
import java.io.*;

public class AddressBook extends MIDlet implements CommandListener{
    private Display display;
    
    //数据流
    ByteArrayInputStream bis;
    ByteArrayOutputStream bos;
    DataInputStream dis;
    DataOutputStream dos;
    
    String dbname = "AddressBook" ;          		//记录存储集的名称
    String current = "" ;                      		//当前screen
    Form addForm = new Form("New Address") ;    	//添加新地址screen
    TextField txtName = new TextField("Name","", 20 , TextField.ANY); //姓名
    TextField txtTel = new TextField("Tel","", 10, TextField.NUMERIC);//地址
    
    //构造函数
    public AddressBook(){
        display = Display.getDisplay(this);
        bis = null;
        bos = null;
        dis = null;
        dos = null;
    }
    
    public void startApp()
    {
        Command add = new Command("Save",Command.SCREEN,1) ;
        Command back = new Command("Back",Command.SCREEN,2) ;
        addForm.append(txtName) ;
        addForm.append(txtTel) ;
        addForm.addCommand(add) ;
        addForm.addCommand(back) ;
        addForm.setCommandListener(this) ;
    
        //显示主界面
        MainForm() ;
    }
    
    //显示主界面
    public void MainForm()
    {
        //选择列表
        List myList = new List("My Address Book", Choice.IMPLICIT) ;
        myList.append("All Addresses", null) ;
        myList.append("New Address", null) ;
        myList.setCommandListener(this) ;
        current = "MainForm" ;
        display.setCurrent(myList) ;
    }
    
    //列举所有地址记录
    public void listAllForm()
    {
        //地址列表
        List myList = new List("All Addresses", Choice.EXCLUSIVE) ;
        Command back = new Command("Back", Command.SCREEN,2) ;
        myList.addCommand(back) ;
        myList.setCommandListener(this) ;
        
        try{
            RecordStore rs = RecordStore.openRecordStore(dbname, true) ;
            RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
            
            //记录存储中没有记录
            if(re.numRecords() == 0)
            {
                myList.append("No addresses!",null) ;
                current = "ListAllForm" ;
                display.setCurrent(myList) ;
                return ;
            }
            //遍历记录存储中的所有记录
            while(re.hasNextElement()) {
                byte tmp[] = re.nextRecord() ;
                bis = new ByteArrayInputStream(tmp);
                dis = new DataInputStream(bis); 
                myList.append(dis.readUTF(), null) ;
            }
            rs.closeRecordStore();
        }catch(Exception e){
            System.out.println("List all Form Wrong!");
			e.printStackTrace();
        }
        //删除地址记录
        Command del = new Command("Delete", Command.SCREEN, 1);
        //察看地址详情
        Command info = new Command("Detail", Command.SCREEN, 1) ;
        myList.addCommand(del);
        myList.addCommand(info) ;
        current = "ListAllForm" ;
        display.setCurrent(myList) ;
    }
    
    //显示添加地址记录界面
    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("Back")) {
            MainForm() ;
        }
        //保存记录
        if(c.getLabel().equals("Save")) {
            addData() ;
            listAllForm() ;
        }
        //察看详情
        if(c.getLabel().equals("Detail")) {
            List tmp = (List) s ;
            searchData(tmp.getString(tmp.getSelectedIndex())) ;
        }
        //删除记录
        if(c.getLabel().equals("Delete")) {
            List tmp = (List) s;
            deleteData(tmp.getString(tmp.getSelectedIndex())) ;
        }
        
    }
    
    //显示地址记录的详细信息
    public void searchData(String name)
    {
        Form f = new Form("Detail") ;
        Command back = new Command("Back",Command.SCREEN,1) ;
        f.addCommand(back) ;
        f.setCommandListener(this) ;
        try
        {
            RecordStore rs = RecordStore.openRecordStore(dbname, false) ;
            RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
            while(re.hasNextElement())
            {
                byte tmp[] = re.nextRecord() ;
                bis = new ByteArrayInputStream(tmp);
                dis = new DataInputStream(bis); 
                String rName = dis.readUTF();
                String rTel = dis.readUTF();
                
                if(rName.equals(name))
                {
                    f.append("Name: \n") ;
                    f.append(rName+"\n") ;
                    f.append("Tel: \n") ;
                    f.append(rTel+"\n") ;
                    display.setCurrent(f) ;
                    return ;
                }
            }
            rs.closeRecordStore() ;
        }catch(Exception e){
            System.out.println("Detail information Error:" + e.getMessage());
        }
    }
    
    
    //删除地址记录
    public void deleteData(String name)
    {
        try
        {
            RecordStore rs = RecordStore.openRecordStore(dbname, false) ;
            RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
            while(re.hasNextElement())
            {
                byte tmp[] = re.nextRecord();
                bis = new ByteArrayInputStream(tmp);
                dis = new DataInputStream(bis); 
                String rName = dis.readUTF();
                String rTel = dis.readUTF();
                if(rName.equals(name))
                {
                    //根据记录ID删除
                    rs.deleteRecord(dis.readInt());
                    //显示所有记录
                    listAllForm();
               }
            }
            re.destroy();
            rs.closeRecordStore() ;
        }catch(Exception e){
            System.out.println("Delete Error:" + e.getMessage());
        }
    }
    
    //向记录存储集中添加记录
    public void addData()
    {
        String name = txtName.getString() ;
        String tel = txtTel.getString() ;
        
        try
        {
            RecordStore rs = RecordStore.openRecordStore(dbname, false) ;
            bos = new ByteArrayOutputStream();
            dos = new DataOutputStream(bos);
            dos.writeUTF(name);                     	//保存姓名
            dos.writeUTF(tel);                      	//保存电话
            dos.writeInt(rs.getNextRecordID());     		//保存记录ID
            dos.flush();
            byte tmp[] = bos.toByteArray();
            rs.addRecord(tmp, 0, tmp.length) ;
            rs.closeRecordStore() ;
        }catch(Exception e) {
            System.out.println("Add data error: " + e.getMessage());
        }
    }
    
    public void pauseApp(){
    }

    public void destroyApp(boolean unconditional){
    }
}

⌨️ 快捷键说明

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