Day25—综合练习

📅 发布时间:2026/7/7 2:51:32 👁️ 浏览次数:
Day25—综合练习
综合练习1-制造假数据public class Test1 { public static void main(String[] args) throws IOException { /* 制造假数据 获取姓氏https://hanyu.baidu.com/shici/detail?pid0b2f26d4c0ddb3ee693fdb1137ee1b0dfromkg0 获取男生名字http://www.haoming8.cn/baobao/10881.html 获取女生名字http://www.haoming8.cn/baobao/7641.html */ //1.定义变量记录网址 String familyNameNet https://hanyu.baidu.com/shici/detail?pid0b2f26d4c0ddb3ee693fdb1137ee1b0dfromkg0; String boyNameNet http://www.haoming8.cn/baobao/10881.html; String girlNameNet http://www.haoming8.cn/baobao/7641.html; //2.爬取数据,把网址上所有的数据拼接成一个字符串 String familyNameStr webCrawler(familyNameNet); String boyNameStr webCrawler(boyNameNet); String girlNameStr webCrawler(girlNameNet); //3.通过正则表达式把其中符合要求的数据获取出来 ArrayListString familyNameTempList getData(familyNameStr,(.{4})(|。),1); ArrayListString boyNameTempList getData(boyNameStr,([\\u4E00-\\u9FA5]{2})(、|。),1); ArrayListString girlNameTempList getData(girlNameStr,(.. ){4}..,0); //4.处理数据 //familyNameTempList姓氏 //处理方案把每一个姓氏拆开并添加到一个新的集合当中 ArrayListString familyNameList new ArrayList(); for (String str : familyNameTempList) { //str 赵钱孙李 周吴郑王 冯陈褚卫 蒋沈韩杨 for (int i 0; i str.length(); i) { char c str.charAt(i); familyNameList.add(c ); } } //boyNameTempList男生的名字 //处理方案去除其中的重复元素 ArrayListString boyNameList new ArrayList(); for (String str : boyNameTempList) { if(!boyNameList.contains(str)){ boyNameList.add(str); } } //girlNameTempList女生的名字 //处理方案把里面的每一个元素用空格进行切割得到每一个女生的名字 ArrayListString girlNameList new ArrayList(); for (String str : girlNameTempList) { String[] arr str.split( ); for (int i 0; i arr.length; i) { girlNameList.add(arr[i]); } } //5.生成数据 //姓名唯一-性别-年龄 ArrayListString list getInfos(familyNameList, boyNameList, girlNameList, 70, 50); Collections.shuffle(list); //6.写出数据 BufferedWriter bw new BufferedWriter(new FileWriter(myiotest\\names.txt)); for (String str : list) { bw.write(str); bw.newLine(); } bw.close(); } /* * 作用 * 获取男生和女生的信息张三-男-23 * * 形参 * 参数一装着姓氏的集合 * 参数二装着男生名字的集合 * 参数三装着女生名字的集合 * 参数四男生的个数 * 参数五女生的个数 * */ public static ArrayListString getInfos(ArrayListString familyNameList,ArrayListString boyNameList,ArrayListString girlNameList, int boyCount,int girlCount){ //1.生成男生不重复的名字 HashSetString boyhs new HashSet(); while (true){ if(boyhs.size() boyCount){ break; } //随机 Collections.shuffle(familyNameList); Collections.shuffle(boyNameList); boyhs.add(familyNameList.get(0) boyNameList.get(0)); } //2.生成女生不重复的名字 HashSetString girlhs new HashSet(); while (true){ if(girlhs.size() girlCount){ break; } //随机 Collections.shuffle(familyNameList); Collections.shuffle(girlNameList); girlhs.add(familyNameList.get(0) girlNameList.get(0)); } //3.生成男生的信息并添加到集合当中 ArrayListString list new ArrayList(); Random r new Random(); //【18 ~ 27】 for (String boyName : boyhs) { //boyName依次表示每一个男生的名字 int age r.nextInt(10) 18; list.add(boyName -男- age); } //4.生成女生的信息并添加到集合当中 //【18 ~ 25】 for (String girlName : girlhs) { //girlName依次表示每一个女生的名字 int age r.nextInt(8) 18; list.add(girlName -女- age); } return list; } /* * 作用根据正则表达式获取字符串中的数据 * 参数一 * 完整的字符串 * 参数二 * 正则表达式 * 参数三 * 获取数据 * 0获取符合正则表达式所有的内容 * 1获取正则表达式中第一组数据 * 2获取正则表达式中第二组数据 * ...以此类推 * * 返回值 * 真正想要的数据 * * */ private static ArrayListString getData(String str, String regex,int index) { //1.创建集合存放数据 ArrayListString list new ArrayList(); //2.按照正则表达式的规则去获取数据 Pattern pattern Pattern.compile(regex); //按照pattern的规则到str当中获取数据 Matcher matcher pattern.matcher(str); while (matcher.find()){ list.add(matcher.group(index)); } return list; } /* * 作用 * 从网络中爬取数据把数据拼接成字符串返回 * 形参 * 网址 * 返回值 * 爬取到的所有数据 * */ public static String webCrawler(String net) throws IOException { //1.定义StringBuilder拼接爬取到的数据 StringBuilder sb new StringBuilder(); //2.创建一个URL对象 URL url new URL(net); //3.链接上这个网址 //细节保证网络是畅通的而且这个网址是可以链接上的。 URLConnection conn url.openConnection(); //4.读取数据 InputStreamReader isr new InputStreamReader(conn.getInputStream()); int ch; while ((ch isr.read()) ! -1){ sb.append((char)ch); } //5.释放资源 isr.close(); //6.把读取到的数据返回 return sb.toString(); } }综合练习2-随机点名器public class Student { private String name; private String gender; private int age; private double weight; public Student() { } public Student(String name, String gender, int age, double weight) { this.name name; this.gender gender; this.age age; this.weight weight; } /** * 获取 * * return name */ public String getName() { return name; } /** * 设置 * * param name */ public void setName(String name) { this.name name; } /** * 获取 * * return gender */ public String getGender() { return gender; } /** * 设置 * * param gender */ public void setGender(String gender) { this.gender gender; } /** * 获取 * * return age */ public int getAge() { return age; } /** * 设置 * * param age */ public void setAge(int age) { this.age age; } /** * 获取 * * return weight */ public double getWeight() { return weight; } /** * 设置 * * param weight */ public void setWeight(double weight) { this.weight weight; } public String toString() { return name - gender - age - weight; } }public class Test { public static void main(String[] args) throws IOException { //1.把文件中所有的学生信息读取到内存中 ArrayListStudent list new ArrayList(); BufferedReader br new BufferedReader(new FileReader(myiotest\\src\\com\\itheima\\myiotest6\\names.txt)); String line; while((line br.readLine()) ! null){ String[] arr line.split(-); Student stu new Student(arr[0],arr[1],Integer.parseInt(arr[2]),Double.parseDouble(arr[3])); list.add(stu); } br.close(); //2.计算权重的总和 double weight 0; for (Student stu : list) { weight weight stu.getWeight(); } //3.计算每一个人的实际占比 //[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1] double[] arr new double[list.size()]; int index 0; for (Student stu : list) { arr[index] stu.getWeight() / weight; index; } //4.计算每一个人的权重占比范围 for (int i 1; i arr.length; i) { arr[i] arr[i] arr[i - 1]; } //5.随机抽取 //获取一个0.0~1.0之间的随机数 double number Math.random(); //判断number在arr中的位置 //二分查找法 //方法回返回 - 插入点 - 1 //获取number这个数据在数组当中的插入点位置 int result -Arrays.binarySearch(arr, number) - 1; Student stu list.get(result); System.out.println(stu); //6.修改当前学生的权重 double w stu.getWeight() / 2; stu.setWeight(w); //7.把集合中的数据再次写到文件中 BufferedWriter bw new BufferedWriter(new FileWriter(myiotest\\src\\com\\itheima\\myiotest6\\names.txt)); for (Student s : list) { bw.write(s.toString()); bw.newLine(); } bw.close(); } }综合练习3-登陆注册public class Test { public static void main(String[] args) throws IOException { /* 需求写一个登陆小案例添加锁定账号功能 步骤 将正确的用户名和密码手动保存在本地的userinfo.txt文件中。 保存格式为:usernamezhangsanpassword123count0 让用户键盘录入用户名和密码 比较用户录入的和正确的用户名密码是否一致 如果一致则打印登陆成功 如果不一致则打印登陆失败连续输错三次被锁定 */ //1.读取正确的用户名和密码 BufferedReader br new BufferedReader(new FileReader(myiotest\\src\\com\\itheima\\myiotest8\\userinfo.txt)); String line br.readLine();//usernamezhangsanpassword123count0 br.close(); String[] userInfo line.split(); String[] arr1 userInfo[0].split(); String[] arr2 userInfo[1].split(); String[] arr3 userInfo[2].split(); String rightUsername arr1[1]; String rightPassword arr2[1]; //count:表示用户连续输错的次数 int count Integer.parseInt(arr3[1]); //2.用户键盘录入用户名和密码 Scanner sc new Scanner(System.in); System.out.println(请输入用户名); String username sc.nextLine(); System.out.println(请输入密码); String password sc.nextLine(); //3.比较 if (rightUsername.equals(username) rightPassword.equals(password) count 3) { System.out.println(登陆成功); writeInfo(username rightUsername password rightPassword count0); } else { count; if (count 3) { System.out.println(登陆失败,还剩下 (3 - count) 次机会); } else { System.out.println(用户账户被锁定); } writeInfo(username rightUsername password rightPassword count count); } } /* * 作用 * 写出一个字符串到本地文件中 * 参数 * 要写出的字符串 * */ public static void writeInfo(String content) throws IOException { BufferedWriter bw new BufferedWriter(new FileWriter(myiotest\\src\\com\\itheima\\myiotest8\\userinfo.txt)); bw.write(content); bw.close(); } }综合练习4-存档和读档综合练习5-游戏配置propertiesproperties时一个双列集合集合拥有Map集合所有的特点重点有一些特有的方法可以把集合中的数据按照键值对的形式写到配置文件中。也可以把配置文件中的数据读到集合中来。public class GameJFrame extends JFrame implements KeyListener, ActionListener { //JFrame 界面窗体 //子类呢也表示界面窗体 //规定GameJFrame这个界面表示的就是游戏的主界面 //以后跟游戏相关的所有逻辑都写在这个类中 //创建一个二维数组 //目的用来管理数据 //加载图片的时候会根据二维数组中的数据进行加载 int[][] data new int[4][4]; //记录空白方块在二维数组中的位置 int x 0; int y 0; //定义一个变量记录当前展示图片的路径 String path puzzlegame\\image\\animal\\animal3\\; //定义一个二维数组存储正确的数据 int[][] win { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0} }; //定义变量用来统计步数 int step 0; //创建选项下面的条目对象 JMenuItem girl new JMenuItem(美女); JMenuItem animal new JMenuItem(动物); JMenuItem sport new JMenuItem(运动); JMenuItem replayItem new JMenuItem(重新游戏); JMenuItem reLoginItem new JMenuItem(重新登录); JMenuItem closeItem new JMenuItem(关闭游戏); JMenu saveJMenu new JMenu(存档); JMenu loadJMenu new JMenu(读档); JMenuItem saveItem0 new JMenuItem(存档0(空)); JMenuItem saveItem1 new JMenuItem(存档1(空)); JMenuItem saveItem2 new JMenuItem(存档2(空)); JMenuItem saveItem3 new JMenuItem(存档3(空)); JMenuItem saveItem4 new JMenuItem(存档4(空)); JMenuItem loadItem0 new JMenuItem(读档0(空)); JMenuItem loadItem1 new JMenuItem(读档1(空)); JMenuItem loadItem2 new JMenuItem(读档2(空)); JMenuItem loadItem3 new JMenuItem(读档3(空)); JMenuItem loadItem4 new JMenuItem(读档4(空)); JMenuItem accountItem new JMenuItem(公众号); //创建随机对象 Random r new Random(); public GameJFrame() { //初始化界面 initJFrame(); //初始化菜单 initJMenuBar(); //初始化数据打乱 initData(); //初始化图片根据打乱之后的结果去加载图片 initImage(); //让界面显示出来建议写在最后 this.setVisible(true); } //初始化数据打乱 private void initData() { //1.定义一个一维数组 int[] tempArr {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //2.打乱数组中的数据的顺序 //遍历数组得到每一个元素拿着每一个元素跟随机索引上的数据进行交换 Random r new Random(); for (int i 0; i tempArr.length; i) { //获取到随机索引 int index r.nextInt(tempArr.length); //拿着遍历到的每一个数据跟随机索引上的数据进行交换 int temp tempArr[i]; tempArr[i] tempArr[index]; tempArr[index] temp; } /* * * 5 6 8 9 * 10 11 15 1 * 4 7 12 13 * 2 3 0 14 * * 5 6 8 9 10 11 15 1 4 7 12 13 2 3 0 14 * */ //4.给二维数组添加数据 //遍历一维数组tempArr得到每一个元素把每一个元素依次添加到二维数组当中 for (int i 0; i tempArr.length; i) { if (tempArr[i] 0) { x i / 4; y i % 4; } data[i / 4][i % 4] tempArr[i]; } } //初始化图片 //添加图片的时候就需要按照二维数组中管理的数据添加图片 private void initImage() { //清空原本已经出现的所有图片 this.getContentPane().removeAll(); if (victory()) { //显示胜利的图标 JLabel winJLabel new JLabel(new ImageIcon(C:\\Users\\moon\\IdeaProjects\\basic-code\\puzzlegame\\image\\win.png)); winJLabel.setBounds(203, 283, 197, 73); this.getContentPane().add(winJLabel); } JLabel stepCount new JLabel(步数 step); stepCount.setBounds(50, 30, 100, 20); this.getContentPane().add(stepCount); //路径分为两种 //绝对路径一定是从盘符开始的。C:\ D\ //相对路径不是从盘符开始的 //相对路径相对当前项目而言的。 aaa\\bbb //在当前项目下去找aaa文件夹里面再找bbb文件夹。 //细节 //先加载的图片在上方后加载的图片塞在下面。 //外循环 --- 把内循环重复执行了4次。 for (int i 0; i 4; i) { //内循环 --- 表示在一行添加4张图片 for (int j 0; j 4; j) { //获取当前要加载图片的序号 int num data[i][j]; //创建一个JLabel的对象管理容器 JLabel jLabel new JLabel(new ImageIcon(path num .jpg)); //指定图片位置 jLabel.setBounds(105 * j 83, 105 * i 134, 105, 105); //给图片添加边框 //0:表示让图片凸起来 //1表示让图片凹下去 jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED)); //把管理容器添加到界面中 this.getContentPane().add(jLabel); } } //添加背景图片 JLabel background new JLabel(new ImageIcon(puzzlegame\\image\\background.png)); background.setBounds(40, 40, 508, 560); //把背景图片添加到界面当中 this.getContentPane().add(background); //刷新一下界面 this.getContentPane().repaint(); } private void initJMenuBar() { //创建整个的菜单对象 JMenuBar jMenuBar new JMenuBar(); //创建菜单上面的两个选项的对象 功能 关于我们 JMenu functionJMenu new JMenu(功能); JMenu aboutJMenu new JMenu(关于我们); JMenu changeImage new JMenu(更换图片); //把5个存档添加到saveJMenu中 saveJMenu.add(saveItem0); saveJMenu.add(saveItem1); saveJMenu.add(saveItem2); saveJMenu.add(saveItem3); saveJMenu.add(saveItem4); //把5个读档添加到loadJMenu中 loadJMenu.add(loadItem0); loadJMenu.add(loadItem1); loadJMenu.add(loadItem2); loadJMenu.add(loadItem3); loadJMenu.add(loadItem4); //把美女动物运动添加到更换图片当中 changeImage.add(girl); changeImage.add(animal); changeImage.add(sport); //将更换图片重新游戏重新登录关闭游戏存档读档添加到“功能”选项当中 functionJMenu.add(changeImage); functionJMenu.add(replayItem); functionJMenu.add(reLoginItem); functionJMenu.add(closeItem); functionJMenu.add(saveJMenu); functionJMenu.add(loadJMenu); //将公众号添加到关于我们当中 aboutJMenu.add(accountItem); //绑定点击事件 girl.addActionListener(this); animal.addActionListener(this); sport.addActionListener(this); replayItem.addActionListener(this); reLoginItem.addActionListener(this); closeItem.addActionListener(this); accountItem.addActionListener(this); saveItem0.addActionListener(this); saveItem1.addActionListener(this); saveItem2.addActionListener(this); saveItem3.addActionListener(this); saveItem4.addActionListener(this); loadItem0.addActionListener(this); loadItem1.addActionListener(this); loadItem2.addActionListener(this); loadItem3.addActionListener(this); loadItem4.addActionListener(this); //将菜单里面的两个选项添加到菜单当中 jMenuBar.add(functionJMenu); jMenuBar.add(aboutJMenu); //读取存档信息修改菜单上表示的内容 getGameInfo(); //给整个界面设置菜单 this.setJMenuBar(jMenuBar); } public void getGameInfo(){ //1.创建File对象表示所有存档所在的文件夹 File file new File(puzzlegame\\save); //2.进入文件夹获取到里面所有的存档文件 File[] files file.listFiles(); //3.遍历数组得到每一个存档 for (File f : files) { //f 依次表示每一个存档文件 //获取每一个存档文件中的步数 GameInfo gi null; try { ObjectInputStream ois new ObjectInputStream(new FileInputStream(f)); gi (GameInfo)ois.readObject(); ois.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } //获取到了步数 int step gi.getStep(); //把存档的步数同步到菜单当中 //save0 --- 0 //save1 --- 1 //... //获取存档的文件名 save0.data String name f.getName(); //获取当存档的序号索引 int index name.charAt(4) - 0; //修改菜单上所表示的文字信息 saveJMenu.getItem(index).setText(存档 index ( step )步); loadJMenu.getItem(index).setText(存档 index ( step )步); } } private void initJFrame() { //设置界面的宽高 this.setSize(603, 680); //设置界面的标题 this.setTitle(拼图单机版 v1.0); //设置界面置顶 this.setAlwaysOnTop(true); //设置界面居中 this.setLocationRelativeTo(null); //设置关闭模式 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //取消默认的居中放置只有取消了才会按照XY轴的形式添加组件 this.setLayout(null); //给整个界面添加键盘监听事件 this.addKeyListener(this); } Override public void keyTyped(KeyEvent e) { } //按下不松时会调用这个方法 Override public void keyPressed(KeyEvent e) { int code e.getKeyCode(); if (code 65) { //把界面中所有的图片全部删除 this.getContentPane().removeAll(); //加载第一张完整的图片 JLabel all new JLabel(new ImageIcon(path all.jpg)); all.setBounds(83, 134, 420, 420); this.getContentPane().add(all); //加载背景图片 //添加背景图片 JLabel background new JLabel(new ImageIcon(puzzlegame\\image\\background.png)); background.setBounds(40, 40, 508, 560); //把背景图片添加到界面当中 this.getContentPane().add(background); //刷新界面 this.getContentPane().repaint(); } } //松开按键的时候会调用这个方法 Override public void keyReleased(KeyEvent e) { //判断游戏是否胜利如果胜利此方法需要直接结束不能再执行下面的移动代码了 if (victory()) { //结束方法 return; } //对上下左右进行判断 //左37 上38 右39 下40 int code e.getKeyCode(); System.out.println(code); if (code 37) { System.out.println(向左移动); if (y 3) { return; } //逻辑 //把空白方块右方的数字往左移动 data[x][y] data[x][y 1]; data[x][y 1] 0; y; //每移动一次计数器就自增一次。 step; //调用方法按照最新的数字加载图片 initImage(); } else if (code 38) { System.out.println(向上移动); if (x 3) { //表示空白方块已经在最下方了他的下面没有图片再能移动了 return; } //逻辑 //把空白方块下方的数字往上移动 //xy 表示空白方块 //x 1 y 表示空白方块下方的数字 //把空白方块下方的数字赋值给空白方块 data[x][y] data[x 1][y]; data[x 1][y] 0; x; //每移动一次计数器就自增一次。 step; //调用方法按照最新的数字加载图片 initImage(); } else if (code 39) { System.out.println(向右移动); if (y 0) { return; } //逻辑 //把空白方块左方的数字往右移动 data[x][y] data[x][y - 1]; data[x][y - 1] 0; y--; //每移动一次计数器就自增一次。 step; //调用方法按照最新的数字加载图片 initImage(); } else if (code 40) { System.out.println(向下移动); if (x 0) { return; } //逻辑 //把空白方块上方的数字往下移动 data[x][y] data[x - 1][y]; data[x - 1][y] 0; x--; //每移动一次计数器就自增一次。 step; //调用方法按照最新的数字加载图片 initImage(); } else if (code 65) { initImage(); } else if (code 87) { data new int[][]{ {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0} }; initImage(); } } //判断data数组中的数据是否跟win数组中相同 //如果全部相同返回true。否则返回false public boolean victory() { for (int i 0; i data.length; i) { //i : 依次表示二维数组 data里面的索引 //data[i]依次表示每一个一维数组 for (int j 0; j data[i].length; j) { if (data[i][j] ! win[i][j]) { //只要有一个数据不一样则返回false return false; } } } //循环结束表示数组遍历比较完毕全都一样返回true return true; } Override public void actionPerformed(ActionEvent e) { //获取当前被点击的条目对象 Object obj e.getSource(); //判断 if (obj replayItem) { System.out.println(重新游戏); //计步器清零 step 0; //再次打乱二维数组中的数据 initData(); //重新加载图片 initImage(); } else if (obj reLoginItem) { System.out.println(重新登录); //关闭当前的游戏界面 this.setVisible(false); //打开登录界面 new LoginJFrame(); } else if (obj closeItem) { System.out.println(关闭游戏); //直接关闭虚拟机即可 System.exit(0); } else if (obj accountItem) { System.out.println(公众号); //创建一个弹框对象 JDialog jDialog new JDialog(); //创建一个管理图片的容器对象JLabel JLabel jLabel new JLabel(new ImageIcon(puzzlegame\\image\\about.png)); //设置位置和宽高 jLabel.setBounds(0, 0, 258, 258); //把图片添加到弹框当中 jDialog.getContentPane().add(jLabel); //给弹框设置大小 jDialog.setSize(344, 344); //让弹框置顶 jDialog.setAlwaysOnTop(true); //让弹框居中 jDialog.setLocationRelativeTo(null); //弹框不关闭则无法操作下面的界面 jDialog.setModal(true); //让弹框显示出来 jDialog.setVisible(true); } else if (obj girl) { System.out.println(girl); //下列代码重复了自己思考一下能否抽取成一个方法呢 int number r.nextInt(13) 1; path puzzlegame\\image\\girl\\girl number \\; //计步器清零 step 0; //再次打乱二维数组中的数据 initData(); //重新加载图片 initImage(); } else if (obj animal) { System.out.println(animal); //下列代码重复了自己思考一下能否抽取成一个方法呢 int number r.nextInt(8) 1; path puzzlegame\\image\\girl\\girl number \\; //计步器清零 step 0; //再次打乱二维数组中的数据 initData(); //重新加载图片 initImage(); } else if (obj sport) { System.out.println(sport); //下列代码重复了自己思考一下能否抽取成一个方法呢 int number r.nextInt(10) 1; path puzzlegame\\image\\girl\\girl number \\; //计步器清零 step 0; //再次打乱二维数组中的数据 initData(); //重新加载图片 initImage(); } else if (obj saveItem0 || obj saveItem1 || obj saveItem2 || obj saveItem3 || obj saveItem4) { //获取当前是哪个存档被点击了获取其中的序号 JMenuItem item (JMenuItem) obj; String str item.getText(); int index str.charAt(2) - 0; //直接把游戏的数据写到本地文件中 try { ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(puzzlegame\\save\\save index .data)); GameInfo gi new GameInfo(data, x, y, path, step); IoUtil.writeObj(oos, true, gi); } catch (IOException ioException) { ioException.printStackTrace(); } //修改一下存档item上的展示信息 //存档0(XX步) item.setText(存档 index ( step 步)); //修改一下读档item上的展示信息 loadJMenu.getItem(index).setText(存档 index ( step 步)); } else if (obj loadItem0 || obj loadItem1 || obj loadItem2 || obj loadItem3 || obj loadItem4) { //获取当前是哪个读档被点击了获取其中的序号 JMenuItem item (JMenuItem) obj; String str item.getText(); int index str.charAt(2) - 0; GameInfo gi null; try { //可以到本地文件中读取数据 ObjectInputStream ois new ObjectInputStream(new FileInputStream(puzzlegame\\save\\save index .data)); gi (GameInfo)ois.readObject(); ois.close(); } catch (IOException ioException) { ioException.printStackTrace(); } catch (ClassNotFoundException classNotFoundException) { classNotFoundException.printStackTrace(); } data gi.getData(); path gi.getPath(); step gi.getStep(); x gi.getX(); y gi.getY(); //重新刷新界面加载游戏 initImage(); } } }