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

📄 recorddata3.java

📁 一本介绍手机游戏开发的书中的源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class recordData3 extends MIDlet implements CommandListener
{
   Display display;
   Form    form1;
   Form    form2;
   Form    form3;
   Form    form4;
   RecordStore rs;
   Alert   alert;
   TextField tf1;
   TextField tf2;
   TextField tf3;
   TextField tf4;
   String  dataInfor;
   int     size;
   Command okCommand;
   Command checkCommand;
   Command deleteCommand;
   Command deleteOKCommand;
   Command saveCommand;
   Command selectCommand;
   Command backCommand;
   Command exitCommand;

   public recordData3()
   {
      alert=new Alert("通讯录运行结果");
      display=Display.getDisplay(this);
      try{
          rs=RecordStore.openRecordStore("RecordD",true);  
      }catch(Exception ex){}
      okCommand = new Command("新增通讯录", Command.OK, 2);
      checkCommand = new Command("查询通讯录", Command.OK, 2);
      deleteCommand = new Command("删除通讯录", Command.OK, 2);
      saveCommand = new Command("确认储存", Command.OK, 2);
      selectCommand = new Command("确认查询", Command.OK, 2);
      deleteOKCommand = new Command("确认删除", Command.OK, 2);
      backCommand = new Command("返回", Command.OK, 2);
      exitCommand = new Command("离开", Command.EXIT, 1);

      form1=new Form("通讯录");
      form1.append("欢迎进入个人通讯录");
      form1.addCommand(okCommand);
      form1.addCommand(checkCommand);
      form1.addCommand(deleteCommand);
      form1.addCommand(exitCommand);
      form1.setCommandListener(this); 

      form2=new Form("请输入要储存的数据");
      tf1=new TextField("名字", null, 10, TextField.ANY);
      tf2=new TextField("电话", null, 10, TextField.PHONENUMBER);
      form2.append(tf1);
      form2.append(tf2);
      form2.addCommand(saveCommand);
      form2.addCommand(backCommand);
      form2.setCommandListener(this);

      form3=new Form("请输入数据信息");
      tf3=new TextField("查询第几条记录", null, 10, TextField.NUMERIC);
      form3.append(tf3);
      form3.addCommand(selectCommand);
      form3.addCommand(backCommand);
      form3.setCommandListener(this);

      form4=new Form("请输入要删除的数据");
      tf4=new TextField("删除第几条记录", null, 10, TextField.NUMERIC);
      form4.append(tf4);
      form4.addCommand(deleteOKCommand);
      form4.addCommand(backCommand);
      form4.setCommandListener(this);
   }

   public void startApp()
   {
      display.setCurrent(form1);       
   }

   public void pauseApp(){}

   public void destroyApp(boolean unconditional){}

   public void commandAction(Command cmd, Displayable disp)
   {
      if (cmd == okCommand)
      {
          display.setCurrent(form2);  
      }
       else if (cmd == saveCommand){
          dataInfor=tf1.getString()+"-TEL:"+tf2.getString();
          if(tf1.getString().trim()!="" && tf2.getString().trim()!="")
            try{
                rs.addRecord(dataInfor.getBytes(),0,dataInfor.getBytes().length);
            }catch(Exception ex){}
          alert.setTimeout(2000);
          alert.setString("成功添加 "+dataInfor); 
          display.setCurrent(alert,form1);
      }
       else if (cmd == checkCommand){
          display.setCurrent(form3); 
      }
       else if (cmd == selectCommand){
          size=0;
          try{
              byte[] datas=rs.getRecord(Integer.parseInt(tf3.getString()));
              dataInfor=new String(datas);
              size=rs.getRecordSize(Integer.parseInt(tf3.getString()));
          }catch(Exception ex){
              dataInfor="查询无记录";
          }
          alert.setTimeout(Alert.FOREVER);
          alert.setString(dataInfor+"  记录使用的内存大小为:"+size); 
          display.setCurrent(alert,form1);         
      }   
       else if (cmd == deleteCommand){
          display.setCurrent(form4); 
      }          
       else if (cmd == deleteOKCommand){
          try{
              byte[] DelDatas=rs.getRecord(Integer.parseInt(tf4.getString()));
              rs.deleteRecord(Integer.parseInt(tf4.getString()));
              dataInfor=new String(DelDatas);
          }catch(Exception ex){
              dataInfor="无记录无法删除";
          }
          alert.setTimeout(Alert.FOREVER);
          alert.setString(dataInfor+"已经成功删除"); 
          display.setCurrent(alert,form1);  
      }       
       else if (cmd == backCommand){
          startApp();
      }
       else if (cmd == exitCommand){
         try{
             rs.closeRecordStore();
         }catch(Exception ex){}
         destroyApp(false);
         notifyDestroyed();
      } 
   }  
}

⌨️ 快捷键说明

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