12:29:46/* * KCB.java * * Created on 2008年5月14日, 上午8:58 */ package kcb; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import java.io.*; import java.lang.*; import java.util.*; public class KCB extends MIDlet implements CommandListener { String[] days = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"}; //记录存储中可供选择的课程名 int choosenum = 14; String[] choose = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}; String search = null; String currentFrm, currentday, currentclass; //用于存储课程信息 String[] subject = {null, null, null, null}; String[] subName = {null, null, null, null}; //用于存储上课时间 String[] oldtime = {null, null, null, null}; StringItem strAbout; private static final String menuText = "课程表功能简介:/n(1)查看每天的课程信息;/n(2)设置每节课的上课时间;/n(3)设置课程信息;/n(4)设置闹钟提醒开关是否打开"; Command cmdExit, cmdSelect, cmdBack, cmdDetail, cmdSave, cmdDel, cmdModify; Display display; List lstMain, lstWeek, lstSettings, lstSub, lstClockOpened; List lstMon, lstTue, lstWed, lstThu, lstFri, lstSat, lstSun, lstSubject; Form frmTimeSet, frmDetail, frmSubAppend, frmhelp; RecordStore dbMon, dbTue, dbWed, dbThu, dbFri, dbSat, dbSun, timetable, subinfo, dbClockOpened; TextField txtClass1, txtClass2, txtClass3, txtClass4; TextField txtSubName, txtTeacher, txtClassroom; DateField dftime1, dftime2, dftime3, dftime4; Date runtime1_date, runtime2_date, runtime3_date, runtime4_date; long runtime1_long, runtime2_long, runtime3_long, runtime4_long; Ticker newsTicker; final int Forever = 0; final int Append = 0; final int Update = 1; int delID; int modifyID; int modifySub; int ClockOpened; boolean changesub = false; //face的作用:标识是否已经显示了封面的信息 int face; Timer timer1 = new Timer(); Timer timer2 = new Timer(); Image[] mainImage = {createImage("/Icons/11.png"), createImage("/Icons/12.png"), createImage("/Icons/13.png"), }; Image[] weekImage = {createImage("/Icons/21.png"), createImage("/Icons/22.png"), createImage("/Icons/21.png"), createImage("/Icons/22.png"), createImage("/Icons/21.png"), createImage("/Icons/22.png"), createImage("/Icons/21.png"), }; Image[] settingsImage = {createImage("/Icons/31.png"), createImage("/Icons/32.png"), createImage("/Icons/33.png"), }; public KCB() { display = Display.getDisplay(this); newsTicker = new Ticker("感谢您使用移动课表!"); cmdExit = new Command("退出", Command.EXIT, 1); cmdSelect = new Command("选择", Command.SCREEN, 1); cmdBack = new Command("返回", Command.BACK, 1); cmdDetail = new Command("详细信息", Command.SCREEN, 1); cmdSave = new Command("保存", Command.SCREEN, 1); cmdDel = new Command("删除", Command.SCREEN, 1); cmdModify = new Command("修改课程", Command.SCREEN, 1); } public void startApp() { face = 1; showMainMenu(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } //事件处理方法,用于响应用户的操作 public void commandAction(Command c, Displayable s) { if (c == cmdExit && s == lstMain) { //设定闹钟 setClock(); System.out.println("07"); System.gc(); destroyApp(true); notifyDestroyed(); } //如果在主界面单击"选择"按钮 if (c == cmdSelect && s == lstMain) { if (lstMain.getSelectedIndex() == 0) { //显示星期界面 System.out.println("08"); showWeek(); } else if ((lstMain.getSelectedIndex() == 1)) { //显示设置区界面 System.out.println("09"); showSettings(); } else if ((lstMain.getSelectedIndex() == 2)) { //显示帮助界面 System.out.println("10"); showHelp(); } } /**************第二层菜单***************/ //如果在帮助界面单击"返回"按钮 if (c == cmdBack && s == frmhelp) { //显示主界面 showMainMenu(); } //如果在星期界面单击"返回"按钮 if (c == cmdBack && s == lstWeek) { //显示主界面 showMainMenu(); } //如果在星期界面单击"选择"按钮 else if (c == cmdSelect && s == lstWeek) { //根据所选的是星期几分别显示当天的大致课程信息 switch (lstWeek.getSelectedIndex()) { case 0://选择了"星期一" showMonday(); break; case 1: showTuesday(); break; case 2: showWednesday(); break; case 3: showThursday(); break; case 4: showFriday(); break; case 5: showSaturday(); break; case 6: showSunday(); break; default: } } //如果在设置界面单击"返回"按钮 if (c == cmdBack && s == lstSettings) { //显示主界面 showMainMenu(); } //如果在设置界面单击"选择"按钮 else if (c == cmdSelect && s == lstSettings) { if (lstSettings.getSelectedIndex() == 0) //显示上课时间设置界面 { showTimeSet(); } else if (lstSettings.getSelectedIndex() == 1) //显示课程信息设置界面 { showSubInfo(); } else if (lstSettings.getSelectedIndex() == 2) //显示上课闹钟提醒开关设置界面 { showClockOpened(); } } /********第三层菜单********/ //闹钟开关 //如果在设置上课闹钟提醒开关界面单击“选择”按钮 if (c == cmdSelect && s == lstClockOpened) { int selectedindex = lstClockOpened.getSelectedIndex(); //选项0为记录存储中原来的数据,选1表示有变化 //因为List中,程序总是把表示记录存储中原来状态的选项放在List的最前面 if (selectedindex == 1) { //写入记录存储 recUpdate(dbClockOpened, 1, lstClockOpened.getString(1)); } //关闭dbClockOpened记录存储 try { dbClockOpened.closeRecordStore(); } catch (Exception e) { } //返回,并显示设置区界面 showSettings(); } //如果在设置上课闹钟提醒开关界面单击“返回”按钮 else if (c == cmdBack && s == lstClockOpened) { try { dbClockOpened.closeRecordStore(); } catch (Exception e) { } //显示设置界面 showSettings(); } //上课时间(Level 3) //如果在设置上课时间的界面单击“”按钮,则必须保存设置的上课时间 if (c == cmdBack && s == frmTimeSet) { //获取Long 型的上课时间 runtime1_long = (dftime1.getDate()).getTime(); runtime2_long = (dftime2.getDate()).getTime(); runtime3_long = (dftime3.getDate()).getTime(); runtime4_long = (dftime4.getDate()).getTime(); //将上课时间转换为String 类型 String a1 = Long.toString(runtime1_long); String a2 = Long.toString(runtime2_long); String a3 = Long.toString(runtime3_long); String a4 = Long.toString(runtime4_long); //判断是否修改了上课时间 boolean changed = false; if ((a1).equals(oldtime[0]) == false) { changed = true; } if ((a2).equals(oldtime[1]) == false) { changed = true; } if ((a3).equals(oldtime[2]) == false) { changed = true; } if ((a4).equals(oldtime[3]) == false) { changed = true; } //如果修改了上课时间,则必须保存新的上课时间 if (changed) { //保存上课时间到timetable记录存储中 if (recUpdate(timetable, 1, a1)); if (recUpdate(timetable, 2, a2)); if (recUpdate(timetable, 3, a3)); if (recUpdate(timetable, 4, a4)); } //关闭 timetable 的记录存储 try { timetable.closeRecordStore(); } catch (Exception e) { } // 置0 for (int i = 0; i < 4; i++) { oldtime[i] = null; } runtime1_long = 0; runtime2_long = 0; runtime3_long = 0; runtime4_long = 0; currentFrm = null; //返回,并显示设置区界面 showSettings(); } //如果在添加新课程界面单击“返回”按钮 if (c == cmdBack && s == frmSubAppend) { //返回,并显示课程信息设置界面 showSubInfo(); } //如果在添加新课程界面单击“保存”按钮,则必须将可趁信息保存下来 if (c == cmdSave && s == frmSubAppend) { boolean saved = true; getchoose(); for (int i = 1; i <= choosenum; i++) { System.out.println("1100"); if ((txtSubName.getString()).equals(choose[i]) || (txtSubName.getString() == "") || choose[choosenum] != "") { System.out.println("1101"); showAlert("注意", "课程名不能重名,不能为空,或已经存满14门课!", 2000); saved = false; } } //如果需要保存 if (saved) { //保存课程信息 doSave(); showAlert("注意", "课程信息存储成功!", 1500); delID = 0; modifyID = 0; } //返回,并显示课程信息设置界面 showSubInfo(); } //如果在显示课程详细信息界面单击“返回”按钮 if (c == cmdBack && s == frmDetail) { //修改课程信息的情况 if (currentFrm == "请选择") { boolean changed = false; //如果用户更改了“课程名”信息 if (subject[0].equals(txtSubName.getString()) == false) { changed = true; getchoose(); //遍历查找新增加的课程名是否重复 for (int i = 1; i <= choosenum; i++) { //课程已经存在 if ((txtSubName.getString()).equals(choose[i])) { changed = false; } } //如果课程名不重复 if (changed) { delID = 0; //保存课程信息 doSave(); //保持数据的一致性 keepRecright(modifyID); } } //如果用户更改了“教师名”或“教室”信息 else { if (subject[1].equals(txtTeacher.getString()) == false) { changed = true; } else if (subject[2].equals(txtClassroom.getString()) == false) { changed = true; } if (changed) { delID = 0; //保存课程信息 doSave(); //保持数据的一致性 keepRecright(modifyID); } } //置0 subject[0] = null; subject[1] = null; subject[2] = null; subject[3] = null; freeChoose(); modifyID = 0; //返回,并显示课程信息设置界面 showSubInfo(); } //退出闹钟提醒界面的情况 else if (currentFrm == "闹钟") { destroyApp(true); notifyDestroyed(); } else { showBack(); } } //如果在显示课程详细信息界面单击“删除”按钮 if (c == cmdDel && s == frmDetail) { modifyID = 0; //删除课程信息 doDelete(delID); delID = 0; showSubInfo(); } //如果用户按钮选择显示某天课程表中某门课程的详细信息 if (c == cmdDetail && (s == lstMon || s == lstTue || s == lstWed || s == lstThu || s == lstFri || s == lstSat || s == lstSun)) { String newSub = null; if (s == lstMon) { newSub = lstMon.getString(lstMon.getSelectedIndex()).trim(); if (lstMon.getSelectedIndex() == 0) { currentclass = "第一节"; } else if (lstMon.getSelectedIndex() == 1) { currentclass = "第二节"; } else if (lstMon.getSelectedIndex() == 2) { currentclass = "第三节"; } else if (lstMon.getSelectedIndex() == 3) { currentclass = "第四节"; } } else if (s == lstTue) { newSub = lstTue.getString(lstTue.getSelectedIndex()).trim(); if (lstTue.getSelectedIndex() == 0) { currentclass = "第一节"; } else if (lstTue.getSelectedIndex() == 1) { currentclass = "第二节"; } else if (lstTue.getSelectedIndex() == 2) { currentclass = "第三节"; } else if (lstTue.getSelectedIndex() == 3) { currentclass = "第四节"; } } else if (s == lstWed) { newSub = lstWed.getString(lstWed.getSelectedIndex()).trim(); if (lstWed.getSelectedIndex() == 0) { currentclass = "第一节"; } else if (lstWed.getSelectedIndex() == 1) { currentclass = "第二节"; } else if (lstWed.getSelectedIndex() == 2) { currentclass = "第三节"; } else if (lstWed.getSelectedIndex() == 3) { currentclass = "第四节"; } } else if (s == lstThu) { newSub = lstThu.getString(lstThu.getSelectedIndex()).trim(); if (lstThu.getSelectedIndex() == 0) { currentclass = "第一节"; } else if (lstThu.getSelectedIndex() == 1) { currentclass = "第二节"; } else if (lstThu.getSelectedIndex() == 2) { currentclass = "第三节"; } else if (lstThu.getSelectedIndex() == 3) { currentclass = "第四节"; } } else if (s == lstFri) { newSub = lstFri.getString(lstFri.getSelectedIndex()).trim(); if (lstFri.getSelectedIndex() == 0) { currentclass = "第一节"; } else if (lstFri.getSelectedIndex() == 1) { currentclass = "第二节"; } else if (lstFri.getSelectedIndex() == 2) { currentclass = "第三节"; } else if (lstFri.getSelectedIndex() == 3) { currentclass = "第四节"; } } else if (s == lstSat) { newSub = lstSat.getString(lstSat.getSelectedIndex()).trim(); if (lstSat.getSelectedIndex() == 0) { currentclass = "第一节"; } else if (lstSat.getSelectedIndex() == 1) { currentclass = "第二节"; } else if (lstSat.getSelectedIndex() == 2) { currentclass = "第三节"; } else if (lstSat.getSelectedIndex() == 3) { currentclass = "第四节"; } } else if (s == lstSun) { newSub = lstSun.getString(lstSun.getSelectedIndex()).trim(); if (lstSun.getSelectedIndex() == 0) { currentclass = "第一节"; } else if (lstSun.getSelectedIndex() == 1) { currentclass = "第二节"; } else if (lstSun.getSelectedIndex() == 2) { currentclass = "第三节"; } else if (lstSun.getSelectedIndex() == 3) { currentclass = "第四节"; } } //newSub保存要查询的课程名 if (newSub.substring(5).equals("没课")) { showAlert("信息", "没课", 1500); } else { getchoose(); for (int i = 1; i <= choosenum; i++) { if (choose[i].equals(newSub.substring(5))) { showDetail(i); } } } } //如果用户按钮选择修改某天课程表中的某门课程 else if (c == cmdModify && (s == lstMon || s == lstTue || s == lstWed || s == lstThu || s == lstFri || s == lstSat || s == lstSun)) { //用modifySub 记录下要修改的是第几节课 if (s == lstMon) { modifySub = lstMon.getSelectedIndex() + 1; } else if (s == lstTue) { modifySub = lstTue.getSelectedIndex() + 1; } else if (s == lstWed) { modifySub = lstWed.getSelectedIndex() + 1; } else if (s == lstThu) { modifySub = lstThu.getSelectedIndex() + 1; } else if (s == lstFri) { modifySub = lstFri.getSelectedIndex() + 1; } else if (s == lstSat) { modifySub = lstSat.getSelectedIndex() + 1; } else if (s == lstSun) { modifySub = lstSun.getSelectedIndex() + 1; } //修改某节可所上的课程 modifySubject(); } //如果用户在显示某一天的概要课程信息是单击了“返回”按钮 else if (c == cmdBack && (s == lstMon || s == lstTue || s == lstWed || s == lstThu || s == lstFri || s == lstSat || s == lstSun)) { //返回,并显示星期信息 showWeek(); } //如果在设置课程信息的界面单击“返回”按钮 if (c == cmdBack && s == lstSub) { //显示设置区界面 showSettings(); } //如果单击“选择”按钮,则需设置课程信息(Level 3) else if (c == cmdSelect && s == lstSub) { String strValue = new String(lstSub.getString(lstSub.getSelectedIndex())); //如果用户单击“添加新课程”按钮 if (strValue.equals("添加新课程")) { System.out.println("14"); //显示添加新课程界面 showSubAppend(); } //否则,则修改某门课的课程信息 else { int selectedItem = lstSub.getSelectedIndex(); int i = 1, j = 0; //遍历查找用户选择的是哪门课 while (true) { if (i == selectedItem) { j = 1; //显示该课程的详细信息 System.out.println("15"); showDetail(i); break; } else { i++; } if (j == 1) { break; } } } } //如果在显示课程列表是单击“返回”按钮 if (c == cmdBack && s == lstSubject) { showBack(); } //如果在显示课程列表是单击“选择”按钮 else if (c == cmdSelect && s == lstSubject) { //newSub 记录的是修改成的课程名称 String newSub = lstSubject.getString(lstSubject.getSelectedIndex()); changesub = false; //用frm表示当前的选择的是星期几 int frm = 0; if (currentFrm == "星期一") { frm = 0; } if (currentFrm == "星期二") { frm = 1; } if (currentFrm == "星期三") { frm = 2; } if (currentFrm == "星期四") { frm = 3; } if (currentFrm == "星期五") { frm = 4; } if (currentFrm == "星期六") { frm = 5; } if (currentFrm == "星期日") { frm = 6; //根据要修改的是星期几的第几节课分别修改不同的记录存储 } switch (frm) { case 0: //修改星期一的第modifySub节课为newSub if (recUpdate(dbMon, modifySub, newSub)); try { dbMon.closeRecordStore(); } catch (Exception e) { } for (int i = 0; i < 4; i++) { subName[i] = null; subject[i] = null; } showBack(); break; case 1: //修改星期二的第modifySub节课为newSub if (recUpdate(dbTue, modifySub, newSub)); try { dbTue.closeRecordStore(); } catch (Exception e) { } for (int i = 0; i < 4; i++) { subName[i] = null; subject[i] = null; } showBack(); break; case 2: //修改星期三的第modifySub节课为newSub if (recUpdate(dbWed, modifySub, newSub)); try { dbWed.closeRecordStore(); } catch (Exception e) { } for (int i = 0; i < 4; i++) { subName[i] = null; subject[i] = null; } showBack(); break; case 3: //修改星期四的第modifySub节课为newSub if (recUpdate(dbThu, modifySub, newSub)); try { dbThu.closeRecordStore(); } catch (Exception e) { } for (int i = 0; i < 4; i++) { subName[i] = null; subject[i] = null; } showBack(); break; case 4: //修改星期五的第modifySub节课为newSub if (recUpdate(dbFri, modifySub, newSub)); try { dbFri.closeRecordStore(); } catch (Exception e) { } for (int i = 0; i < 4; i++) { subName[i] = null; subject[i] = null; } showBack(); break; case 5: //修改星期六的第modifySub节课为newSub if (recUpdate(dbSat, modifySub, newSub)); try { dbSat.closeRecordStore(); } catch (Exception e) { } for (int i = 0; i < 4; i++) { subName[i] = null; subject[i] = null; } showBack(); break; case 6: //修改星期日的第modifySub节课为newSub if (recUpdate(dbSun, modifySub, newSub)); try { dbSun.closeRecordStore(); } catch (Exception e) { } for (int i = 0; i < 4; i++) { subName[i] = null; subject[i] = null; } showBack(); break; } } } /************************************菜单类*************************************/ //显示主界面(level1) public void showMainMenu() { display = Display.getDisplay(this); String[] items = {"课程表", "设置区", "关于"}; lstMain = new List("主界面", List.IMPLICIT, items, mainImage); lstMain.setTicker(newsTicker); lstMain.addCommand(cmdExit); lstMain.addCommand(cmdSelect); lstMain.setCommandListener(this); if (face == 1) { Image photo = null; try { photo = Image.createImage("/Icons/face.png"); } catch (java.io.IOException err) { } Alert a = new Alert("封面!", "编写人员:郭燕春,/n指导老师:吴延昌", photo, null); a.setTimeout(1000); display.setCurrent(a, lstMain); face = 0; } else { display.setCurrent(lstMain); } } //显示帮助界面(level2) public void showHelp() { frmhelp = new Form("关于本软件"); strAbout = new StringItem(null, menuText); frmhelp.append(strAbout); frmhelp.addCommand(cmdBack); frmhelp.setCommandListener(this); display.setCurrent(frmhelp); } //显示星期界面(level2) public void showWeek() { lstWeek = new List("请选择一天", List.IMPLICIT, days, weekImage); lstWeek.addCommand(cmdBack); lstWeek.addCommand(cmdSelect); lstWeek.setCommandListener(this); display.setCurrent(lstWeek); } //显示设置区(level2) public void showSettings() { String[] items = {"上课时间", "课程信息", "上课提醒"}; lstSettings = new List("设置区:", List.IMPLICIT, items, settingsImage); lstSettings.addCommand(cmdBack); lstSettings.addCommand(cmdSelect); lstSettings.setCommandListener(this); display.setCurrent(lstSettings); } //--------------------------------------------------------------------- //---------------------------课程表查询区------------------------------. //--------------------------------------------------------------------- //显示星期一的大致课程信息(Level3) public void showMonday() { boolean opened = false; boolean existingOrNot = false; dbMon = null; try { dbMon = RecordStore.openRecordStore("dbMon", opened); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } if (existingOrNot == false) { try { dbMon = RecordStore.openRecordStore("dbMon", true); } catch (Exception e) { opened = false; } } try { dbMon = RecordStore.openRecordStore("dbMon", false); opened = true; } catch (Exception e) { opened = false; } if (opened) { currentday = "星期一"; lstMon = new List("星期一课程:", List.IMPLICIT); for (int i = 0; i < 4; i++) { subject[i] = getRec(dbMon, i + 1); if (subject[i].equals("")) { subject[i] = "没课"; recUpdate(dbMon, i + 1, subject[i]); } subName[i] = subject[i]; lstMon.append("第" + (i + 1) + "节课:" + subject[i], null); } lstMon.addCommand(cmdBack); lstMon.addCommand(cmdDetail); lstMon.addCommand(cmdModify); lstMon.setCommandListener(this); display.setCurrent(lstMon); currentFrm = "星期一"; } else { showAlert("错误", "打开记录存储或创建记录存储失败", 2500); } } //显示星期二的大致课程信息(Level3) public void showTuesday() { boolean opened = false; boolean existingOrNot = false; dbTue = null; //判断名为dbTue的记录存储是否存在? //存在,则打开并设置existingOrNot为true try { dbTue = RecordStore.openRecordStore("dbTue", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } //如果不存在 if (existingOrNot == false) { //创建新的记录存储dbTue try { dbTue = RecordStore.openRecordStore("dbTue", true); } catch (Exception e) { opened = false; } } //判断记录存储是否存在?如果存在,则找开它 try { dbTue = RecordStore.openRecordStore("dbTue", false); opened = true; } catch (Exception e) { opened = false; } //如果正常打开dbTue if (opened) { currentday = "星期二"; lstTue = new List("星期二课程:", List.IMPLICIT); for (int i = 0; i < 4; i++) { subject[i] = getRec(dbTue, i + 1); //如果某一项记录为空,则将其设置为“没课” if (subject[i].equals("")) { subject[i] = "没课"; recUpdate(dbTue, i + 1, subject[i]); } subName[i] = subject[i]; lstTue.append("第" + (i + 1) + "节课:" + subject[i], null); } lstTue.addCommand(cmdBack); lstTue.addCommand(cmdDetail); lstTue.addCommand(cmdModify); lstTue.setCommandListener(this); display.setCurrent(lstTue); //用currentFrm标识当前界面 currentFrm = "星期二"; } else {//打开失败 showAlert("错误", "打开记录存储或创建记录存储失败!", 2500); } } //显示星期三的大致课程信息(Level3) public void showWednesday() { boolean opened = false; boolean existingOrNot = false; dbWed = null; //判断名为dbWed的记录存储是否存在? //存在,则打开并设置existingOrNot为true try { dbWed = RecordStore.openRecordStore("dbWed", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } //如果不存在 if (existingOrNot == false) { //创建新的记录存储dbWed try { dbWed = RecordStore.openRecordStore("dbWed", true); } catch (Exception e) { opened = false; } } //判断记录存储是否存在?如果存在,则找开它 try { dbWed = RecordStore.openRecordStore("dbWed", false); opened = true; } catch (Exception e) { opened = false; } //如果正常打开dbWed if (opened) { currentday = "星期三"; lstWed = new List("星期三课程:", List.IMPLICIT); for (int i = 0; i < 4; i++) { subject[i] = getRec(dbWed, i + 1); //如果某一项记录为空,则将其设置为“没课” if (subject[i].equals("")) { subject[i] = "没课"; recUpdate(dbWed, i + 1, subject[i]); } subName[i] = subject[i]; lstWed.append("第" + (i + 1) + "节课:" + subject[i], null); } lstWed.addCommand(cmdBack); lstWed.addCommand(cmdDetail); lstWed.addCommand(cmdModify); lstWed.setCommandListener(this); display.setCurrent(lstWed); //用currentFrm标识当前界面 currentFrm = "星期三"; } else {//打开失败 showAlert("错误", "打开记录存储或创建记录存储失败!", 2500); } } //显示星期四的大致课程信息(Level3) public void showThursday() { boolean opened = false; boolean existingOrNot = false; dbThu = null; //判断名为dbThu的记录存储是否存在? //存在,则打开并设置existingOrNot为true try { dbThu = RecordStore.openRecordStore("dbThu", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } //如果不存在 if (existingOrNot == false) { //创建新的记录存储dbWed try { dbThu = RecordStore.openRecordStore("dbThu", true); } catch (Exception e) { opened = false; } } //判断记录存储是否存在?如果存在,则找开它 try { dbThu = RecordStore.openRecordStore("dbThu", false); opened = true; } catch (Exception e) { opened = false; } //如果正常打开dbWed if (opened) { currentday = "星期四"; lstThu = new List("星期四课程:", List.IMPLICIT); for (int i = 0; i < 4; i++) { subject[i] = getRec(dbThu, i + 1); //如果某一项记录为空,则将其设置为“没课” if (subject[i].equals("")) { subject[i] = "没课"; recUpdate(dbThu, i + 1, subject[i]); } subName[i] = subject[i]; lstThu.append("第" + (i + 1) + "节课:" + subject[i], null); } lstThu.addCommand(cmdBack); lstThu.addCommand(cmdDetail); lstThu.addCommand(cmdModify); lstThu.setCommandListener(this); display.setCurrent(lstThu); //用currentFrm标识当前界面 currentFrm = "星期四"; } else {//打开失败 showAlert("错误", "打开记录存储或创建记录存储失败!", 2500); } } //显示星期五的大致课程信息(Level3) public void showFriday() { boolean opened = false; boolean existingOrNot = false; dbThu = null; //判断名为dbFri的记录存储是否存在? //存在,则打开并设置existingOrNot为true try { dbFri = RecordStore.openRecordStore("dbFri", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } //如果不存在 if (existingOrNot == false) { //创建新的记录存储dbFri try { dbFri = RecordStore.openRecordStore("dbFri", true); } catch (Exception e) { opened = false; } } //判断记录存储是否存在?如果存在,则找开它 try { dbFri = RecordStore.openRecordStore("dbFri", false); opened = true; } catch (Exception e) { opened = false; } //如果正常打开dbFri if (opened) { currentday = "星期五"; lstFri = new List("星期五课程:", List.IMPLICIT); for (int i = 0; i < 4; i++) { subject[i] = getRec(dbFri, i + 1); //如果某一项记录为空,则将其设置为“没课” if (subject[i].equals("")) { subject[i] = "没课"; recUpdate(dbFri, i + 1, subject[i]); } subName[i] = subject[i]; lstFri.append("第" + (i + 1) + "节课:" + subject[i], null); } lstFri.addCommand(cmdBack); lstFri.addCommand(cmdDetail); lstFri.addCommand(cmdModify); lstFri.setCommandListener(this); display.setCurrent(lstFri); //用currentFrm标识当前界面 currentFrm = "星期五"; } else {//打开失败 showAlert("错误", "打开记录存储或创建记录存储失败!", 2500); } } //显示星期六的大致课程信息(Level3) public void showSaturday() { boolean opened = false; boolean existingOrNot = false; dbSat = null; //判断名为dbSat的记录存储是否存在? //存在,则打开并设置existingOrNot为true try { dbSat = RecordStore.openRecordStore("dbSat", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } //如果不存在 if (existingOrNot == false) { //创建新的记录存储dbSat try { dbSat = RecordStore.openRecordStore("dbSat", true); } catch (Exception e) { opened = false; } } //判断记录存储是否存在?如果存在,则找开它 try { dbSat = RecordStore.openRecordStore("dbSat", false); opened = true; } catch (Exception e) { opened = false; } //如果正常打开dbSat if (opened) { currentday = "星期六"; lstSat = new List("星期六课程:", List.IMPLICIT); for (int i = 0; i < 4; i++) { subject[i] = getRec(dbSat, i + 1); if (subject[i].equals("")) { subject[i] = "没课"; recUpdate(dbSat, i + 1, subject[i]); } subName[i] = subject[i]; lstSat.append("第" + (i + 1) + "节课:" + subject[i], null); } lstSat.addCommand(cmdBack); lstSat.addCommand(cmdDetail); lstSat.addCommand(cmdModify); lstSat.setCommandListener(this); display.setCurrent(lstSat); currentFrm = "星期六"; } else { showAlert("错误", "打开记录存储或创建记录存储失败!", 2500); } } //显示星期日的大致课程信息(Level3) public void showSunday() { boolean opened = false; boolean existingOrNot = false; dbSun = null; try { dbSun = RecordStore.openRecordStore("dbSun", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } if (existingOrNot == false) { try { dbSun = RecordStore.openRecordStore("dbSun", true); } catch (Exception e) { opened = false; } } try { dbSun = RecordStore.openRecordStore("dbSun", false); opened = true; } catch (Exception e) { opened = false; } if (opened) { currentday = "星期日"; lstSun = new List("星期日课程:", List.IMPLICIT); for (int i = 0; i < 4; i++) { subject[i] = getRec(dbSun, i + 1); //如果某一项记录为空,则将其设置为“没课” if (subject[i].equals("")) { subject[i] = "没课"; recUpdate(dbSun, i + 1, subject[i]); } subName[i] = subject[i]; lstSun.append("第" + (i + 1) + "节课:" + subject[i], null); } lstSun.addCommand(cmdBack); lstSun.addCommand(cmdDetail); lstSun.addCommand(cmdModify); lstSun.setCommandListener(this); display.setCurrent(lstSun); //用currentFrm标识当前界面 currentFrm = "星期日"; } else {//打开失败 showAlert("错误", "打开记录存储或创建记录存储失败!", 2500); } } //--------------------------------------- //------------功能区--------------------- //--------------------------------------- //显示课程信息设置面 public void showSubInfo() { boolean opened = false; boolean existingOrNot = false; subinfo = null; try { subinfo = RecordStore.openRecordStore("subinfo", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } if (existingOrNot == false) { try { subinfo = RecordStore.openRecordStore("subinfo", true); try { byte[] data = "System Reserved".getBytes(); subinfo.addRecord(data, 0, data.length); } catch (Exception e) { opened = false; } } catch (Exception e) { } } try { subinfo = RecordStore.openRecordStore("subinfo", opened); opened = true; } catch (Exception e) { opened = false; } if (opened) { int total = 0; byte[] data; SubjectInfo newSubject = new SubjectInfo(); lstSub = new List("请选择:", List.IMPLICIT); lstSub.append("添加新课程", null); try { total = subinfo.getNumRecords() - 1; } catch (Exception e) { } if (total >= 1) { try { total = subinfo.getNextRecordID(); for (int i = 2; i < total; i++) { try { data = subinfo.getRecord(i); } catch (InvalidRecordIDException e) { continue; } newSubject.changeFromByteArray(data); lstSub.append(newSubject.getSubName(), null); } } catch (RecordStoreException e) { } } lstSub.addCommand(cmdBack); lstSub.addCommand(cmdSelect); lstSub.setCommandListener(this); display.setCurrent(lstSub); currentFrm = "请选择"; } else { showAlert("错误", "创建或打开记录存储失败", Forever); } } //显示课程的详细信息 public void showDetail(int recID) { boolean opened = false; boolean existingOrNot = false; subinfo = null; if (currentFrm == "星期一" || currentFrm == "星期二" || currentFrm == "星期三" || currentFrm == "星期四" || currentFrm == "星期五" || currentFrm == "星期六" || currentFrm == "星期日"); { if (currentclass == "第一节") { search = subName[0]; } else if (currentclass == "第二节") { search = subName[1]; } else if (currentclass == "第三节") { search = subName[2]; } else if (currentclass == "第四节") { search = subName[3]; } else if (currentFrm == "闹钟") { search = subName[0]; } else if (currentFrm == "请选择") { search = lstSub.getString(recID); } try { subinfo = RecordStore.openRecordStore("subinfo", false); opened = true; } catch (Exception e) { opened = false; } if (opened == true) { byte[] data; String search2 = null; int ID = 0; int total = 0; SubjectInfo newSubject = new SubjectInfo(); try { total = subinfo.getNextRecordID(); } catch (Exception e) { } for (int i = 1; i < total; i++) { try { data = subinfo.getRecord(i); newSubject.changeFromByteArray(data); search2 = newSubject.getSubName(); if (search.equals(search2)) { ID = i; break; } recID = subinfo.getNextRecordID(); } catch (Exception e) { } } if (currentFrm == "请选择") { frmDetail = new Form("课程信息设置:"); delID = ID; modifyID = ID; frmDetail.addCommand(cmdDel); } else { if (currentFrm == "闹钟") { frmDetail = new Form("准备上课啦"); } else if (currentday == "星期一") { frmDetail = new Form("星期一课程信息:"); } else if (currentday == "星期二") { frmDetail = new Form("星期二课程信息:"); } else if (currentday == "星期三") { frmDetail = new Form("星期三课程信息:"); } else if (currentday == "星期四") { frmDetail = new Form("星期四课程信息:"); } else if (currentday == "星期五") { frmDetail = new Form("星期五课程信息:"); } else if (currentday == "星期六") { frmDetail = new Form("星期六课程信息:"); } else if (currentday == "星期日") { frmDetail = new Form("星期日课程信息:"); } } txtSubName = new TextField("课程名:", newSubject.getSubName(), 30, TextField.ANY); txtTeacher = new TextField("教师名:", newSubject.getTeacher(), 30, TextField.ANY); txtClassroom = new TextField("教室:", newSubject.getClassroom(), 30, TextField.ANY); if (currentFrm != "请选择") { showTime(); } frmDetail.append(txtSubName); frmDetail.append(txtTeacher); frmDetail.append(txtClassroom); subject[0] = newSubject.getSubName(); subject[1] = newSubject.getTeacher(); subject[2] = newSubject.getClassroom(); frmDetail.addCommand(cmdBack); frmDetail.setCommandListener(this); display.setCurrent(frmDetail); currentclass = null; } else { showAlert("错误", "打开记录存储失败", Forever); } } } //显示添加课程信息界面 public void showSubAppend() { frmSubAppend = new Form("新课程信息:"); txtSubName = new TextField("课程名:", "", 30, TextField.ANY); txtTeacher = new TextField("教师名:", "", 30, TextField.ANY); txtClassroom = new TextField("教室:", "", 30, TextField.ANY); frmSubAppend.append(txtSubName); frmSubAppend.append(txtTeacher); frmSubAppend.append(txtClassroom); frmSubAppend.addCommand(cmdBack); frmSubAppend.addCommand(cmdSave); frmSubAppend.setCommandListener(this); display.setCurrent(frmSubAppend); } //显示上课时间设置界面 public void showTimeSet() { boolean opened = false; boolean existingOrNot = false; timetable = null; //判断名为timetable的记录存储是否存在? //存在,则打开,并设置existingOrNOt为true try { timetable = RecordStore.openRecordStore("timetable", false); existingOrNot = true; } catch(Exception e){ existingOrNot = false; } //如果不存在 if(existingOrNot == false){ //新建记录存储timetable try { timetable = RecordStore.openRecordStore("timetable",true); } catch(Exception e){ opened = false; } } //判断记录存储是否存在?如果存在,则打开它 try { timetable = RecordStore.openRecordStore("timetable",false); opened = true; } catch(Exception e){ opened = false; } //如果能正常打开 if(opened){ //从 timetable中读出上课时间并保存到oldtime数组中 for(int i= 0; i< 4; i++){ oldtime[i]=getRec(timetable,i+1); //若为空,则预设为12:00mid if(oldtime[i] == ""){ oldtime[i] = "0"; } } //将上课时间转换为Long型 runtime1_long = (long)Long.parseLong(oldtime[0]); runtime2_long = (long)Long.parseLong(oldtime[1]); runtime3_long = (long)Long.parseLong(oldtime[2]); runtime4_long = (long)Long.parseLong(oldtime[3]); //构建显示上课时间按的DateField,并加入到frmTiemSet中 dftime1 = new DateField("第一节",DateField.TIME); dftime2 = new DateField("第二节",DateField.TIME); dftime3 = new DateField("第三节",DateField.TIME); dftime4 = new DateField("第四节",DateField.TIME); frmTimeSet = new Form("设置上课时间"); runtime1_date = new Date(runtime1_long); runtime2_date = new Date(runtime2_long); runtime3_date = new Date(runtime3_long); runtime4_date = new Date(runtime4_long); dftime1.setDate(runtime1_date); dftime2.setDate(runtime2_date); dftime3.setDate(runtime3_date); dftime4.setDate(runtime4_date); frmTimeSet.append(dftime1); frmTimeSet.append(dftime2); frmTimeSet.append(dftime3); frmTimeSet.append(dftime4); frmTimeSet.addCommand(cmdBack); frmTimeSet.setCommandListener(this); display.setCurrent(frmTimeSet); } else { showAlert("错误","创建或打开记录存储失败",Forever); } } public void showBack() { if (currentFrm == "星期一") { showMonday(); } else if (currentFrm == "星期二") { showTuesday(); } else if (currentFrm == "星期三") { showWednesday(); } else if (currentFrm == "星期四") { showThursday(); } else if (currentFrm == "星期五") { showFriday(); } else if (currentFrm == "星期六") { showSaturday(); } else if (currentFrm == "星期日") { showSunday(); } else { showWeek(); } } //获取当前年、月、日的总毫秒数 public long getYearMonthDay() { long datelong = 0; Date dater = new Date(); dater = showDate(dater); DateField df = new DateField(null, DateField.DATE); df.setDate(dater); datelong = (df.getDate()).getTime(); return (datelong); } public Date showDate(Date d) { Calendar c = Calendar.getInstance(); c.set(Calendar.MONTH, Calendar.getInstance().get(Calendar.MONTH)); c.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().get(Calendar.DAY_OF_MONTH)); c.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR)); return c.getTime(); } //设定闹钟提醒 public void setClock() { long datelong; datelong = getYearMonthDay(); boolean opened = false; timetable = null; try { timetable = RecordStore.openRecordStore("timetable", false); opened = true; } catch (Exception e) { opened = false; } if (opened) { String classnum; for (int i = 0; i < 4; i++) { oldtime[i] = getRec(timetable, i + 1); if (oldtime[i] == "") { oldtime[i] = "0"; } } runtime1_long = (long) Long.parseLong(oldtime[0]); runtime2_long = (long) Long.parseLong(oldtime[1]); runtime3_long = (long) Long.parseLong(oldtime[2]); runtime4_long = (long) Long.parseLong(oldtime[3]); runtime1_date = new Date(runtime1_long + datelong - 300000); runtime2_date = new Date(runtime2_long + datelong - 300000); runtime3_date = new Date(runtime3_long + datelong - 300000); runtime4_date = new Date(runtime4_long + datelong - 300000); boolean isopened; try { dbClockOpened = RecordStore.openRecordStore("dbClockOpened", false); isopened = true; } catch (Exception e) { isopened = false; } if (isopened) { String s; if ((s = getRec(dbClockOpened, 2)).equals("1")) { for (int i = 0; i < 4; i++) { try { if (i == 0) { classnum = "第一节"; timer1.scheduleAtFixedRate(new AlertTask(classnum, runtime1_long), runtime1_date, 86400000); } else if (i == 1) { classnum = "第二节"; timer1.scheduleAtFixedRate(new AlertTask(classnum, runtime2_long), runtime2_date, 86400000); } else if (i == 2) { classnum = "第三节"; timer1.scheduleAtFixedRate(new AlertTask(classnum, runtime3_long), runtime3_date, 86400000); } else if (i == 3) { classnum = "第四节"; timer1.scheduleAtFixedRate(new AlertTask(classnum, runtime4_long), runtime4_date, 86400000); } } catch (IllegalStateException e) { System.out.println("IllegalStateException or IllegalArgumentException error"); } } } else if ((s = getRec(dbClockOpened, 2)).equals("2")) { for (int i = 0; i < 4; i++) { try { if (i == 0) { classnum = "第一节"; timer2.scheduleAtFixedRate(new AlertTask(classnum, runtime1_long), runtime1_date, 86400000); } else if (i == 1) { classnum = "第二节"; timer2.scheduleAtFixedRate(new AlertTask(classnum, runtime2_long), runtime2_date, 86400000); } else if (i == 2) { classnum = "第三节"; timer2.scheduleAtFixedRate(new AlertTask(classnum, runtime3_long), runtime3_date, 86400000); } else if (i == 1) { classnum = "第四节"; timer2.scheduleAtFixedRate(new AlertTask(classnum, runtime4_long), runtime4_date, 86400000); } } catch (IllegalStateException e) { System.out.println("IllegalStateException or IllegalArgumentException error"); } } } try { dbClockOpened.closeRecordStore(); } catch (Exception e) { } System.out.println("闹钟设置成功是timer" + s); } } try { timetable.closeRecordStore(); } catch (Exception e) { } } //闹钟运行的事件类 class AlertTask extends TimerTask { String classnum = ""; long runclocktime = 0; AlertTask(String classnum, long runclocktime) { this.classnum = classnum; this.runclocktime = runclocktime; } public void run() { boolean isopened; try { dbClockOpened = RecordStore.openRecordStore("dbClockOpened", false); isopened = true; } catch (Exception e) { isopened = false; } long datelong; datelong = getYearMonthDay(); datelong = System.currentTimeMillis() - datelong; if (isopened == true && (datelong - 60000) <= runclocktime && runclocktime >= (datelong + 60000)) { String data = getRec(dbClockOpened, 1); if (isopened == true && (datelong - 60000) <= runclocktime && runclocktime >= (datelong + 60000)) { if (data.equals("开")) { resumeRequest(); runClock(classnum); } else { } } try { dbClockOpened.closeRecordStore(); } catch (Exception e) { } } } } //运行闹钟提醒 public void runClock(String aclass) { currentclass = aclass; String subjectname = null; int classnum = 0; if (currentclass.equals("第一节")) { classnum = 1; } else if (currentclass.equals("第二节")) { classnum = 2; } else if (currentclass.equals("第三节")) { classnum = 3; } else if (currentclass.equals("第四节")) { classnum = 4; } else { } int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1; switch (day) { case 1: try { dbMon = RecordStore.openRecordStore("dbMon", false); } catch (Exception e) { } subjectname = getRec(dbMon, classnum); currentday = "星期一"; break; case 2: try { dbTue = RecordStore.openRecordStore("dbTue", false); } catch (Exception e) { } subjectname = getRec(dbTue, classnum); currentday = "星期二"; break; case 3: try { dbWed = RecordStore.openRecordStore("dbWed", false); } catch (Exception e) { } subjectname = getRec(dbWed, classnum); currentday = "星期三"; break; case 4: try { dbThu = RecordStore.openRecordStore("dbThu", false); } catch (Exception e) { } subjectname = getRec(dbThu, classnum); currentday = "星期四"; break; case 5: try { dbFri = RecordStore.openRecordStore("dbFri", false); } catch (Exception e) { } subjectname = getRec(dbFri, classnum); currentday = "星期五"; break; case 6: try { dbSat = RecordStore.openRecordStore("dbSat", false); } catch (Exception e) { } subjectname = getRec(dbSat, classnum); currentday = "星期六"; break; case 7: try { dbSun = RecordStore.openRecordStore("dbSun", false); } catch (Exception e) { } subjectname = getRec(dbSun, classnum); currentday = "星期日"; break; default: System.out.println("没有这个日期"); } if (!subjectname.equals("没课")) { subName[0] = subjectname; subName[1] = subjectname; subName[2] = subjectname; subName[3] = subjectname; getchoose(); currentFrm = "闹钟"; for (int i = 1; i < 15; i++) { if ((choose[i]).equals(subjectname)) { showDetail(i); } } } } //显示上课闹钟提醒开关的设置界面 public void showClockOpened() { boolean opened = false; boolean existingOrNot = false; dbClockOpened = null; try { dbClockOpened = RecordStore.openRecordStore("dbClockOpened", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } if (existingOrNot == false) { try { dbClockOpened = RecordStore.openRecordStore("dbClockOpened", true); } catch (Exception e) { opened = false; } } try { dbClockOpened = RecordStore.openRecordStore("dbClockOpened", false); opened = true; } catch (Exception e) { opened = false; } if (opened == true) { String[] isopened = {"开", "关"}; Alert a; String data = getRec(dbClockOpened, 1); if (data.equals("开")) { a = new Alert("闹钟提醒开关当前状态", " 当前为开!", null, null); } else if (data.equals("关")) { a = new Alert("闹钟提醒开关当前状态", " 当前为关!", null, null); isopened[0] = "关"; isopened[1] = "开"; } else { a = new Alert("闹钟提醒开关当前状态", " 默认为开!", null, null); recUpdate(dbClockOpened, 1, "开"); } lstClockOpened = new List("闹钟提醒开关设置:", List.IMPLICIT, isopened, null); lstClockOpened.addCommand(cmdBack); lstClockOpened.addCommand(cmdSelect); lstClockOpened.setCommandListener(this); a.setTimeout(1500); display.setCurrent(a, lstClockOpened); } else { showAlert("错误", "创建或打开记录存储失败", Forever); } } //在显示课程详细信息时加上上课时间 public void showTime() { boolean opened = false; timetable = null; try { timetable = RecordStore.openRecordStore("timetable", false); opened = true; } catch (Exception e) { opened = false; } if (opened) { for (int i = 0; i < 4; i++) { oldtime[i] = getRec(timetable, i + 1); if (oldtime[i] == "") { oldtime[i] = "0"; } } runtime1_long = (long) Long.parseLong(oldtime[0]); runtime2_long = (long) Long.parseLong(oldtime[1]); runtime3_long = (long) Long.parseLong(oldtime[3]); runtime4_long = (long) Long.parseLong(oldtime[3]); if (currentclass.equals("第一节")) { dftime1 = new DateField("第一节", DateField.TIME); runtime1_date = new Date(runtime1_long); dftime1.setDate(runtime1_date); frmDetail.append(dftime1); } else if (currentclass.equals("第二节")) { dftime2 = new DateField("第二节", DateField.TIME); runtime2_date = new Date(runtime2_long); dftime2.setDate(runtime2_date); frmDetail.append(dftime2); } else if (currentclass.equals("第三节")) { dftime3 = new DateField("第三节", DateField.TIME); runtime3_date = new Date(runtime3_long); dftime3.setDate(runtime3_date); frmDetail.append(dftime3); } else if (currentclass.equals("第四节")) { dftime4 = new DateField("第四节", DateField.TIME); runtime4_date = new Date(runtime4_long); dftime4.setDate(runtime4_date); frmDetail.append(dftime4); } else { } } else { showAlert("错误", "创建或打开记录存储失败", Forever); } } //对存放课程名的数组choose进行清零 public void freeChoose() { for (int i = 0; i <= choosenum; i++) { choose[i] = ""; } } //获取已经设置的课程的名称,并放到choose数组中 public void getchoose() { boolean opened = false; subinfo = null; for (int i = 0; i <= choosenum; i++) { choose[i] = ""; } //打开名为“subinfo”的记录存储 try { subinfo = RecordStore.openRecordStore("subinfo", false); opened = true; } catch (Exception e) { opened = false; } //如果能正常打开subinfo if (opened) { int total = 0; byte[] data; SubjectInfo newSubject = new SubjectInfo(); try { //已经设置好的课程总数 total = subinfo.getNumRecords() - 1; } catch (Exception e) { showAlert("错误", "读取记录存储出错!", Forever); } if (total >= 1) { int k = 0; try { total = subinfo.getNextRecordID(); //遍历记录存储,取出课程名并保存到choose中 for (int i = 1; i < total; i++) { try { data = subinfo.getRecord(i); } catch (InvalidRecordIDException e) { continue; } newSubject.changeFromByteArray(data); choose[k++] = newSubject.getSubName(); } } catch (RecordStoreException e) { } //关闭记录存储 try { subinfo.closeRecordStore(); } catch (Exception e) { System.out.println("关闭记录存储错误!"); } } } else { showAlert("错误", "创建或打开记录存储subinfo失败!", Forever); } } //修改某节课所上的课程 public void modifySubject() { getchoose(); lstSubject=new List ("课程列表:",List.IMPLICIT); lstSubject.append("没课",null); for(int i=2;i<=choosenum;i++){ if(choose[i]!=""){ lstSubject.append(choose[i],null); } } lstSubject.addCommand(cmdSelect); lstSubject.addCommand(cmdBack); lstSubject.setCommandListener(this); display.setCurrent(lstSubject); //创建图形对象 } private Image createImage(String date) { Image aImage = null; try { aImage = Image.createImage(date); } catch (Exception e) { } return aImage; } //用于显示一个Alert对象, 标题是titel,内容是message,显示模式是Mode public void showAlert(String titel, String message, int mode) { Alert alert = new Alert(titel, message, null, null); if (mode == Forever) { System.out.println("1102"); alert.setTimeout(Alert.FOREVER); } else { System.out.println("1103"); alert.setTimeout(mode); } display.setCurrent(alert); } //进行各个记录存储之间的数据一致性检查 public void keepRecright(int delId) { SubjectInfo delSubject = new SubjectInfo(); byte[] data = null; try { data = subinfo.getRecord(delId); delSubject.changeFromByteArray(data); String delSubName = delSubject.getSubName(); for (int i = 1; i < 4; i++) { if (currentFrm == "请选择" && modifyID != 0) { if (getRec(dbMon, i).equals(subject[0])) { recUpdate(dbMon, i, delSubName); } if (getRec(dbTue, i).equals(subject[0])) { recUpdate(dbTue, i, delSubName); } if (getRec(dbWed, i).equals(subject[0])) { recUpdate(dbWed, i, delSubName); } if (getRec(dbThu, i).equals(subject[0])) { recUpdate(dbThu, i, delSubName); } if (getRec(dbFri, i).equals(subject[0])) { recUpdate(dbFri, i, delSubName); } if (getRec(dbSat, i).equals(subject[0])) { recUpdate(dbSat, i, delSubName); } if (getRec(dbSun, i).equals(subject[0])) { recUpdate(dbSun, i, delSubName); } } else { if (getRec(dbMon, i).equals(delSubName)) { recUpdate(dbMon, i, "没课"); } if (getRec(dbTue, i).equals(delSubName)) { recUpdate(dbTue, i, "没课"); } if (getRec(dbWed, i).equals(delSubName)) { recUpdate(dbWed, i, "没课"); } if (getRec(dbThu, i).equals(delSubName)) { recUpdate(dbThu, i, "没课"); } if (getRec(dbFri, i).equals(delSubName)) { recUpdate(dbFri, i, "没课"); } if (getRec(dbSat, i).equals(delSubName)) { recUpdate(dbSat, i, "没课"); } if (getRec(dbSun, i).equals(delSubName)) { recUpdate(dbSun, i, "没课"); } } } delSubject = null; } catch (Exception e) { System.out.println("error"); } } //对记录存储进行读取操作(subinfo除外) public String getRec(RecordStore rs, int recID) { byte[] data; String result, buffer; Other newclass = null; try { newclass = new Other(); data = rs.getRecord(recID); newclass.changeFromByteArray(data); result = newclass.getnew1(); } catch (Exception e) { result = ""; } return result; } //用于对记录存储进行记录的修改或新增(subinfo除外) public boolean recUpdate(RecordStore rs, int classNum, String subjectName) { boolean result = false; String title = new String(""); String message = new String(""); boolean error = false; Other newclass = new Other(); newclass.write(subjectName); byte[] data = newclass.changeToByteArray(); if (error = false) { try { rs.setRecord(classNum, data, 0, data.length); result = true; } catch (Exception e) { title = "错误"; message = "更新数据失败! 原因:/n" + e.getMessage(); error = true; } } if (error) { showAlert(title, message, Forever); } return result; } //用于对subinfo记录存储中的记录进行删除 public void doDelete(int delID) { boolean existingOrNot = false; subinfo = null; try { subinfo = RecordStore.openRecordStore("subinfo", false); keepRecright(delID); } catch (Exception e) { } try { if (subinfo != null) { subinfo.closeRecordStore(); } } catch (Exception e) { } } //用于新建subinfo记录存储和修改其记录 public void doSave() { boolean existingOrNot = false; subinfo = null; boolean opened = false; //判断subinfo记录存储是否存在?设置existingOrNot遍历 try { subinfo = RecordStore.openRecordStore("subinfo", false); existingOrNot = true; } catch (Exception e) { existingOrNot = false; } //如果subinfo记录存储不存在,则新建 if (existingOrNot == false) { try { subinfo = RecordStore.openRecordStore("subinfo", true); } catch (Exception e) { opened = false; } } //打开subinfo记录存储 try { subinfo = RecordStore.openRecordStore("subinfo", false); opened = true; } catch (Exception e) { opened = false; } //如果能正常打开subinfo if (opened) { SubjectInfo newSubject = null; try { newSubject = new SubjectInfo(); //将数据(课程名、教室名、地点)写入SubjectInfo 类对象newSubject newSubject.write(txtSubName.getString(), txtTeacher.getString(), txtClassroom.getString()); //将数据转换为字节数组类型 byte[] data = newSubject.changeToByteArray(); //新建记录的情况 if (modifyID == 0) { int recordID = newSubject.getRecordID(); recordID = subinfo.addRecord(data, 0, data.length); newSubject.setRecordID(recordID); } //修改记录的情况 else { subinfo.setRecord(modifyID, data, 0, data.length); } //置0 newSubject = null; } catch (Exception e) { showAlert("错误", "保存失败", Forever); } //关闭subinfo记录存储 try { if (subinfo != null) { subinfo.closeRecordStore(); } } catch (Exception e) { } } else { showAlert("错误", "创建或打开记录存储失败!", Forever); } } } //********************存储类区*********************************/ //为了统一处理记录存储中的课程信息的存取而新建的类 class SubjectInfo { private int ID = -1; private String SubName, Teacher, Classroom; //写入信息 public void write(String SubName, String Teacher, String Classroom) { this.SubName = SubName; this.Teacher = Teacher; this.Classroom = Classroom; } public void setRecordID(int ID) { this.ID = ID; } public int getRecordID() { return ID; } //将多个String 类型饿字段“打包”为一个字节数组 public byte[] changeToByteArray() { byte[] data = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeUTF(SubName); dos.writeUTF(Teacher); dos.writeUTF(Classroom); data = baos.toByteArray(); baos.close(); dos.close(); } catch (Exception e) { } return data; } //从一个字节数组“解包”出多个String 类型的字段 public void changeFromByteArray(byte[] data) { try { ByteArrayInputStream bais = new ByteArrayInputStream(data); DataInputStream dis = new DataInputStream(bais); SubName = dis.readUTF(); Teacher = dis.readUTF(); Classroom = dis.readUTF(); bais.close(); dis.close(); } catch (Exception e) { } } public String getSubName() { return SubName; } public String getTeacher() { return Teacher; } public String getClassroom() { return Classroom; } } //为了统一处理记录存储中数据的存取而新建的类 class Other { private int ID = -1; private String new1; public void write(String new1) { this.new1 = new1; } public void setRecordID(int ID) { this.ID = ID; } public int getRecordID() { return ID; } //将String 类型的字段“打包”为一个字节数组 public byte[] changeToByteArray() { byte[] data = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeUTF(new1); data = baos.toByteArray(); baos.close(); dos.close(); } catch (Exception e) { } return data; } //从一个字节数组“解包”出Strings类型的字段 public void changeFromByteArray(byte[] data) { try { ByteArrayInputStream bais = new ByteArrayInputStream(data); DataInputStream dis = new DataInputStream(bais); new1 = dis.readUTF(); bais.close(); dis.close(); } catch (Exception e) { } } public String getnew1() { return new1; } }