2007-01-13

J2ME实现的通讯录程式

关键字: J2ME

     本程序主要利用在J2ME MIDP的RMS功能,演示了如何通过RecordStore对象进行存储数据,并实现一个通讯录程序,程序的主要代码如下:

AddressBook.java的源代码如下:

java 代码
  1. /*  
  2.  
  3.  * Created on 2005-11-4  
  4.  
  5.  *  
  6.  
  7.  * TODO To change the template for this generated file go to  
  8.  
  9.  * Window - Preferences - Java - Code Style - Code Templates  
  10.  
  11.  */  
  12.   
  13.     
  14.   
  15. /**  
  16.  
  17.   *  
  18.  
  19.  * TODO To change the template for this generated type comment go to  
  20.  
  21.  * Window - Preferences - Java - Code Style - Code Templates  
  22.  
  23.  */  
  24.   
  25. import javax.microedition.midlet.*;   
  26.   
  27. //import javax.microedition.io.*;   
  28.   
  29. import javax.microedition.lcdui.*;   
  30.   
  31. import javax.microedition.rms.*;   
  32.   
  33. //import java.util.Enumeration;   
  34.   
  35. //import java.util.Vector;   
  36.   
  37. //import java.io.*;   
  38.   
  39.     
  40.   
  41. /*AddressBook.java  
  42.  
  43.  *演示如何使用RMS的记录储存的管理  
  44.  
  45.  *记录的管理和查询的排序  
  46.  
  47.  *结合lcdui来操作RMS  
  48.  
  49. */  
  50.   
  51. public class AddressBook extends MIDlet implements CommandListener   
  52.   
  53. {   
  54.   
  55.   private Display display=null;   
  56.   
  57.   Command search=null;   
  58.   
  59.   Command quit=null;   
  60.   
  61.   Command delete=null;   
  62.   
  63.   Command addnow=null;       
  64.   
  65.   Command edit=null;   
  66.   
  67.   Command mainmenu=null;   
  68.   
  69.      
  70.   
  71.   List menu=null;   
  72.   
  73.   Form ui_form=null;   
  74.   
  75.   StringItem si=null;   
  76.   
  77.   TextField name=null;   
  78.   
  79.   TextField phone=null;   
  80.   
  81.   TextField nameForEdit=null;   
  82.   
  83.      
  84.   
  85.   RecordStore recordStore=null;//声明记录储存   
  86.   
  87.      
  88.   
  89.   public AddressBook()   
  90.   
  91.   {   
  92.   
  93.       display=Display.getDisplay(this);  //定义Display变量   
  94.   
  95.       quit=new Command("关闭",Command.SCREEN,3);//定义关闭按钮   
  96.   
  97.       search=new Command("查找",Command.SCREEN,2);//定义查询按钮   
  98.   
  99.       delete=new Command("删除",Command.SCREEN,2);//定义删除按钮   
  100.   
  101.       addnow=new Command("增加",Command.SCREEN,2);//定义增加按钮   
  102.   
  103.       edit=new Command("修改",Command.SCREEN,2);//定义修改按钮   
  104.   
  105.       mainmenu=new Command("主菜单",Command.SCREEN,2);//定义主菜单按钮   
  106.   
  107.           //记录储存初始化   
  108.   
  109.       try    
  110.   
  111.       {   
  112.   
  113.           recordStore=RecordStore.openRecordStore("address",true);   
  114.   
  115.       }   
  116.   
  117.       catch(RecordStoreException rse)   
  118.   
  119.       {   
  120.   
  121.           rse.printStackTrace();   
  122.   
  123.       }   
  124.   
  125.   }   
  126.   
  127.   public void startApp()   
  128.   
  129.   {   
  130.   
  131.       menu=new List("电话薄...",List.IMPLICIT);   
  132.   
  133.       menu.append("1.查询",null);   
  134.   
  135.       menu.append("2.增加",null);   
  136.   
  137.       menu.append("3.删除",null);   
  138.   
  139.       menu.append("4.修改",null);   
  140.   
  141.       menu.append("5.关闭",null);   
  142.   
  143.       menu.setCommandListener(this);   
  144.   
  145.       display.setCurrent(menu);   
  146.   
  147.       }   
  148.   
  149.   void searchScreen()  //查询用户界面   
  150.   
  151.   {   
  152.   
  153.       ui_form=new Form("电话薄查询");   
  154.   
  155.        name=new TextField("查询姓名","",50,0);   
  156.   
  157.       ui_form.append(name);   
  158.   
  159.          
  160.   
  161.       ui_form.addCommand(search);   
  162.   
  163.       ui_form.addCommand(quit);   
  164.   
  165.          
  166.   
  167.       ui_form.setCommandListener(this);   
  168.   
  169.          
  170.   
  171.       display.setCurrent(ui_form);   
  172.   
  173.   }   
  174.   
  175.     void addScreen()   
  176.   
  177.     {   
  178.   
  179.       ui_form=new Form("添加");   
  180.   
  181.          
  182.   
  183.       name=new TextField("姓名","",50,0);   
  184.   
  185.       ui_form.append(name);   
  186.   
  187.          
  188.   
  189.       phone=new TextField("电话号码","",50,0);   
  190.   
  191.       ui_form.append(phone);   
  192.   
  193.          
  194.   
  195.       ui_form.addCommand(addnow);   
  196.   
  197.       ui_form.addCommand(quit);   
  198.   
  199.          
  200.   
  201.       ui_form.setCommandListener(this);   
  202.   
  203.          
  204.   
  205.       display.setCurrent(ui_form);   
  206.   
  207.     }   
  208.   
  209.     void editScreen()   
  210.   
  211.     {   
  212.   
  213.       ui_form=new Form("修改");   
  214.   
  215.          
  216.   
  217.       nameForEdit=new TextField("需要修改的姓名","",50,0);   
  218.   
  219.       ui_form.append(nameForEdit);   
  220.   
  221.          
  222.   
  223.       name=new TextField("姓名","",50,0);   
  224.   
  225.       ui_form.append(name);   
  226.   
  227.          
  228.   
  229.       phone=new TextField("电话号码","",50,0);   
  230.   
  231.       ui_form.append(phone);   
  232.   
  233.                  
  234.   
  235.       ui_form.addCommand(edit);   
  236.   
  237.       ui_form.addCommand(quit);   
  238.   
  239.          
  240.   
  241.       ui_form.setCommandListener(this);   
  242.   
  243.          
  244.   
  245.       display.setCurrent(ui_form);   
  246.   
  247.     }   
  248.   
  249.     void deleteScreen()   
  250.   
  251.     {   
  252.   
  253.       ui_form=new Form("删除");   
  254.   
  255.          
  256.   
  257.                  
  258.   
  259.       name=new TextField("姓名","",50,0);   
  260.   
  261.       ui_form.append(name);   
  262.   
  263.          
  264.   
  265.                          
  266.   
  267.       ui_form.addCommand(delete);   
  268.   
  269.       ui_form.addCommand(quit);   
  270.   
  271.          
  272.   
  273.       ui_form.setCommandListener(this);   
  274.   
  275.          
  276.   
  277.       display.setCurrent(ui_form);   
  278.   
  279.     }   
  280.   
  281. public void pauseApp()   
  282.   
  283. {   
  284.   
  285.   menu=null;   
  286.   
  287. }   
  288.   
  289. public void destroyApp(boolean unconditional)   
  290.   
  291. {   
  292.   
  293.   menu=null;   
  294.   
  295.   notifyDestroyed();   
  296.   
  297. }   
  298.   
  299. public void commandAction(Command c,Displayable d)   
  300.   
  301. {   
  302.   
  303.   if(c==quit)   
  304.   
  305.   {   
  306.   
  307.       try  
  308.   
  309.       {   
  310.   
  311.           close();   
  312.   
  313.       }   
  314.   
  315.      
  316.   
  317.       catch(RecordStoreException rse)   
  318.   
  319.       {   
  320.   
  321.           rse.printStackTrace();   
  322.   
  323.       }   
  324.   
  325.       destroyApp(true);   
  326.   
  327.       }   
  328.   
  329.     
  330.   
  331. else if(c==search)   
  332.   
  333. {   
  334.   
  335.   String temp_search=name.getString();   
  336.   
  337.   address_search(temp_search);   
  338.   
  339.      
  340.   
  341. }   
  342.   
  343. else if(c==edit)   
  344.   
  345. {   
  346.   
  347.   String temp_nameForEdit=nameForEdit.getString();   
  348.   
  349.   String temp_name=name.getString();   
  350.   
  351.   String temp_phone=phone.getString();   
  352.   
  353.   address_edit(temp_nameForEdit,temp_name,temp_phone);   
  354.   
  355.   }   
  356.   
  357. else if(c==mainmenu)   
  358.   
  359. {   
  360.   
  361.   startApp();   
  362.   
  363. }   
  364.   
  365. else if (c==delete)   
  366.   
  367. {   

上面的是不完整的,大体体现了一些思路而已

评论
发表评论

您还没有登录,请登录后发表评论

xyh
搜索本博客
博客分类
我的相册
D6559ae8-1ca0-3934-bfe8-0ec70062a94e-thumb
新建 BMP 图像
共 10 张
存档