recorddata4.java

来自「一本介绍手机游戏开发的书中的源代码」· Java 代码 · 共 131 行

JAVA
131
字号
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

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

   public recordData4()
   {
      alert=new Alert("执行结果");
      alert.setTimeout(2000);
      display=Display.getDisplay(this);
      try{
          rs=RecordStore.openRecordStore("RecordE",true);  
      }catch(Exception ex){}
      okCommand = 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(deleteCommand);
      form1.addCommand(exitCommand);
      form1.setCommandListener(this); 

      form2=new Form("请输入储存数据信息");
      tf1=new TextField("名字", null, 10, TextField.ANY);
      tf2=new TextField("公司", null, 10, TextField.ANY);
      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(deleteOKCommand);
      form3.addCommand(backCommand);
      form3.setCommandListener(this);
 
      rs.addRecordListener(this);
   }

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

   public void pauseApp(){}

   public void destroyApp(boolean unconditional){}

   public void recordAdded(RecordStore rs, int recordId)
   {
      alert.setString("成功加入一条记录:"+dataInfor); 
      display.setCurrent(alert,form1);
   }

   public void recordChanged(RecordStore rs, int recordId)
   {
      alert.setString("成功修改一条记录"); 
      display.setCurrent(alert,form1);    
   }

   public void recordDeleted(RecordStore rs, int recordId)
   {
      alert.setString("成功删除一条记录:"+dataInfor); 
      display.setCurrent(alert,form1);
   }

   public void commandAction(Command cmd, Displayable disp)
   {
      if (cmd == okCommand)
      {
          display.setCurrent(form2);  
      }
       else if (cmd == saveCommand){
          dataInfor=tf1.getString()+"-COM:"+tf2.getString();
          if(tf1.getString().trim()!="" && tf2.getString().trim()!="")
            try{
                rs.addRecord(dataInfor.getBytes(),0,dataInfor.getBytes().length);
            }catch(Exception ex){}
      }   
       else if (cmd == deleteCommand){
          display.setCurrent(form3); 
      }          
       else if (cmd == deleteOKCommand){
          try{
              byte[] DelDatas=rs.getRecord(Integer.parseInt(tf3.getString()));
              rs.deleteRecord(Integer.parseInt(tf3.getString()));
              dataInfor=new String(DelDatas);
          }catch(Exception ex){
              dataInfor="无记录无法删除";
          }
      }       
       else if (cmd == backCommand){
          startApp();
      }
       else if (cmd == exitCommand){
         try{
             rs.closeRecordStore();
         }catch(Exception ex){}
         destroyApp(false);
         notifyDestroyed();
      } 
   }  
}

⌨️ 快捷键说明

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