//设置“关闭程序后,结束窗体所在的应用程序” setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } 件
m=5;//初始化行数 n=6;//初始化列数
gradeFile=fileOneGrade;//将“初级记忆排行榜.txt”文件赋给gradeFile
//调用initBlock()方法创建和设置图标
memoryArea.initBlock(m,n,imageName,gradeFile); } 件
m=6;//初始化行数 n=7;//初始化列数
gradeFile=fileTwoGrade;//将“中级记忆排行榜.txt”文件赋给gradeFile
//调用initBlock()方法创建和设置图标
memoryArea.initBlock(m,n,imageName,gradeFile); } 件
m=7;//初始化行数 n=8;//初始化列数
gradeFile=fileThreeGrade;//将“高级记忆排行榜.txt”文件赋给gradeFile
//调用initBlock()方法创建和设置图标
if(event.getSource()==threeGradeItem){
//用户从菜单条点击菜单“选择级别”中的菜单项“高级”,触发事
if(event.getSource()==twoGradeItem){
//用户从菜单条点击菜单“选择级别”中的菜单项“中级”,触发事
//该方法是接口ActionListener的方法,触发ActionEvent事件,用来执public void actionPerformed(ActionEvent event){
if(event.getSource()==oneGradeItem){
//用户从菜单条点击菜单“选择级别”中的菜单项“初级”,触发事
行菜单项发出的有关命令
21
memoryArea.initBlock(m,n,imageName,gradeFile); }
if(event.getSource()==carImageIcon){
//用户从菜单条点击菜单“选择图标”中的菜单项“汽车图标”,触for(int i=0;i imageName[i]=new String(\} //调用方法把存放图像文件名字的imageName数组的引用传递 发事件 给imageFileName memoryArea.setImageName(imageName); //调用initBlock()方法创建和设置imageName数组图标 memoryArea.initBlock(m,n,imageName,gradeFile); } if(event.getSource()==animalImageIcon){ //用户从菜单条点击菜单“选择图标”中的菜单项“动物图标”,触for(int i=0;i imageName[i]=new String(\} 发事件 //调用方法把存放图像文件名字的imageName数组的引用传递给memoryArea.setImageName(imageName); //调用initBlock()方法创建和设置imageName数组图标 memoryArea.initBlock(m,n,imageName,gradeFile); imageFileName } if(event.getSource()==oneGradeResult){ //用户从菜单条点击菜单“查看排行榜”中的菜单项“初级记忆排showDiolag.setGradeFile(fileOneGrade);//调用setGradeFile()//对话框调用showRecord()方法读取gradeFile文件中的成绩,按 行榜”,触发事件 方法将级别文件传递给gradeFile 高低顺序显示在showArea文本区 showDiolag.showRecord(); //设置文本框可见 22 showDiolag.setVisible(true); } if(event.getSource()==twoGradeResult){ //用户从菜单条点击菜单“查看排行榜”中的菜单项“中级记忆排 调 用 行榜”,触发事件 showDiolag.setGradeFile(fileTwoGrade);//setGradeFile()方法将级别文件传递给gradeFile //对话框调用showRecord()方法读取gradeFile文件中的成绩,按高低顺序显示在showArea文本区 showDiolag.showRecord(); //设置文本框可见 showDiolag.setVisible(true); } if(event.getSource()==threeGradeResult){ //用户从菜单条点击菜单“查看排行榜”中的菜单项“高级记忆排行榜”, 触发事件 showDiolag.setGradeFile(fileThreeGrade);//调用setGradeFile()方法将级别文件传递给gradeFile //对话框调用showRecord()方法读取gradeFile文件中的成绩,按高低顺序显示在showArea文本区 showDiolag.showRecord(); //对话框调用showRecord()方法读取gradeFile文件中的成绩,按高低顺序显示在showArea文本区 showDiolag.setVisible(true); } } } 3.2 MemoryTestArea类 代码: class MemoryTestArea extends JPanel implements ActionListener,Runnable { //确定测试区中方块的数量row,col 23 int row,col; //用户的用时,单位是秒 int usedTime=0; //用户找到的具有同样的图标的方块的个数 int success=0; //级别文件 File gradeFile; //数组表allBlockList的每个单元存放着一个Block对象 ArrayList //字符串数组imageFileName中的每个单元是一幅图像文件的文字 String imageFileName[]; //链表openIconList用来存放用户找到的图标相同的方块上的图标 LinkedList //链表openBlockList用来存放用户找到的图标相同的方块 LinkedList //hintButton是注册了ActionEvent事件的监听器,当单击它时,启动JButton hintButton; //文本框组件:显示用户同时(usedTime的值),显示要寻找的方块数 JTextField showUsedTime,hintMessage; //计时器,负责改变usedTime的值 javax.swing.Timer timer; //声明”保存成绩“对话框对象 Record record; //声明“中心”和“南”容器对象 JPanel center,south; //构造方法,构造测试区 MemoryTestArea(){ //使用BorderLayout布局 setLayout(new BorderLayout()); //创建数组表allBlockList对象 allBlockList=new ArrayList hintThread线程。 24 openIconList=new LinkedList //创建显示要寻找的方块数hintMessage对象 hintMessage=new JTextField(); //组件的”对齐方式“为“中间对齐” hintMessage.setHorizontalAlignment(JTextField.CENTER); //组件的“可编辑性'为“不可编辑” hintMessage.setEditable(false); //设置组件的字体 hintMessage.setFont(new Font(\宋体\//创建”中心“对象 center=new JPanel(); //创建”南“对象 south=new JPanel(); //创建”提示“按钮对象 hintButton=new JButton(\提示\//给”提示“按钮注册监听器 hintButton.addActionListener(this); //创建文本框对象,可以输入8个字符 showUsedTime=new JTextField(8); //组件的“可编辑性'为“不可编辑” showUsedTime.setEditable(false); //组件”对齐方式“为”中间对齐“ showUsedTime.setHorizontalAlignment(JTextField.CENTER); //在容器的南增加标签”用时“ south.add(new JLabel(\用时:\//在容器的南增加文本框”显示用户用时“ south.add(showUsedTime); //在容器的“南”增加标签”提示图标位置(导致用时增加):“ south.add(new JLabel(\提示图标位置(导致用时增加):\//在容器的南增加按钮”提示“ south.add(hintButton); 25