教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 基础教育 >

《java程序大作业 - 记忆测试系统》 - 图文(4)

来源:网络收集 时间:2026-05-31
导读: MemoryTestArea创建的测试区 2. UML图 MemoryTestArea类是javax.swing包中JPanel容器的子类,实现了ActionListener和Runnable接口,所创建的对象:memoryArea是MemoryGame类的最重要的成员之一,作为一个容器添加到

MemoryTestArea创建的测试区

2. UML图 MemoryTestArea类是javax.swing包中JPanel容器的子类,实现了ActionListener和Runnable接口,所创建的对象:memoryArea是MemoryGame类的最重要的成员之一,作为一个容器添加到MemoryGame窗口的中心。标明MemoryTestGame类的主要成员变量、方法以及和MemoryGame类之间组合关系的UML图如下图所示。

JPanel ActionListener Runnable

MemoryTestArea MemoryGame row,col,usedTime,success:int hintTread:Thread memoryArea: gradeFile:File hintButton:JButton MemoryTestArea allBlockList:ArrayList showUsedTime:JTextFiled imageFileName:String[] hintMessage:JTextFiled openIconList:LinkedList timer:Timer openBlockList:LinkedList record:Record MemoryTestArea():无类型 initBlock(int,int,String[],File):void setImageName(String[]):void actionPerformed(ActionEvent):void run():void

以下是UML图中有关数据和方法的详细说明。

11 / 30

1)成员变量

row和col的值确定测试区中方块的数量。MemoryTestArea类创建的对象memoryArea是MemoryGame中的成员,memoryArea对象通过调用initBlock(int,int,String[],File)方法将MemoryGame类中的m和n值传递给row和col。

usedTime是用户的用时,单位是秒,iconNum是用户鼠标单击的次数,单位是次。 Success的值是用户找到的具有相同的图标方块的个数。

gradeFile是级别文件,memoryArea对象通过调用initBlock(int,int,String[],File)方法见MemoryGame类中的gradeFile的引用传递给memoryArea对象中的gradeFile。

数组表allBlockList单元的个数是row和col的乘积,它的每个单元存放着一个Block对象。memoryArea对象在调用intBlock(int,int,String[],File)方法时完成对allBlockList单元初始化,即创建单元中的Block对象。

字符串数组imageFileName中的每个单元是一幅图像文件的文字。memoryArea对象通过调用setImageName(String[])方法将MemoryGame中存放图像文件名字的imageName数据的引用传递给imageFileName。 链表openIconList用来存放用户找到的图标相同的方块上的图标。 链表openBlockList用来存放用户找到的图标相同的方块。

hintThread是用Thread类创建的线程对象,用来提示测试区中的Block上的图标,提示方式是将测试区中的各个Block上图标持续显示1200毫秒。

hintButton是注册了ActionEvent事件的监视器,当单击它时,启动hintThread线程。

showUsedTime显示用户的用时,即显示usedTime的值,showIconNum是显示用户单击鼠标的次数,即显示showIconNum的值。

record负责提供保存成绩的界面,是一个对话框,默认不见。当用户寻找出级别所要求的相同的图标方块的数目后,该对话框可见,用户可以在该对话框中输入姓名,并保存所输入的姓名和usedTime的值到gradeFile指定的级别文件中。 2) 方法

12 / 30

MemoryTestArea()是构造方法,创建memoryArea对象时需使用该构造方法。

initBlock(int,int,String[],File)方法。memoryArea对象调用该方法将参数的值传递给row、col、imageFileName和gradeFile,并依据这些值设置allBlockList数组表的大小,然后创建allBlockList的单元中的Block对象,并设置Block对象上的图标。

setImageName(String[])方法。memoryArea对象调用该方法可以将MemoryGame中存放图像文件名字的imageName数组的引用传递给imageFileName。当用户单击MemoryGame主类窗口的carImageIcon和animalImageIcon菜单项时,memoryArea对象将调用该方法把存放图像文件名字的imageName数组的引用传递给imageFileName。

actionPerformed(ActionEvent)方法。该方法是MemoryArea类实现的ActionListener接口中的方法。memoryArea中的每个Block对象注册了ActionEvent事件监视器,当用户单击memoryArea中的某个Block对象时,actionPerformed(ActionEvent)方法将被调用执行,所执行的主要操作是:如果该Block对象未显示图标,并且该Block对象设置的图标和openIconList的存放的图标相同,就将该图标添加到链表openIconList中同时将该Block对象上图标显示出来,并将success的值增1;如果该Block对象未显示图标,并且该Block对象设置的图标和OpenIconList的中存放的图标不相同,就将openIconList清空,然后再将该图标添加到链表openIconList中,同时将该Block对象上图标显示出来,并将success的值设置成1。当用户单击hintButton按钮时,actionPerformed(ActionEvent)方法将被调用执行,执行的主要操作是启动hintThread线程。

run()方法。该方法是MemoryArea类实现Runnable接口中的方法,启动hintThread线程后,该方法将执行,其主要操作是将测试区中的未显示图标的各个Block对象上的图标持续显示1200毫秒。

3. (MemoryTestArea.java)

import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; import java.io.*;

import java.applet.AudioClip; import java.applet.Applet; import java.net.*;

public class MemoryTestArea extends JPanel implements ActionListener,Runnable{ //

Runnbale int row, col;

File gradeFile,musicFile; ArrayListallBlockList; String imageFileName[];

LinkedListopenIconList; LinkedListopenBlockList; int success = 0; Thread hintThread; JButton hintButton;

13 / 30

int usedTime = 0;

int iconNum = 0; JTextField showIconNum;

JTextField showUsedTime,hintMessage; javax.swing.Timer timer; Record record; JPanel center,south; MemoryTestArea(){ }

public void initBlock(int m,int n,String name[],File f){

row = m; col = n; gradeFile = f; center.removeAll(); imageFileName = name;

14 / 30

setLayout(new BorderLayout());

allBlockList = new ArrayList(); openIconList = new LinkedList(); openBlockList= new LinkedList(); hintThread = new Thread(this); hintMessage = new JTextField();

hintMessage.setHorizontalAlignment(JTextField.CENTER); hintMessage.setEditable(false);

hintMessage.setFont(new Font(\宋体\,Font.BOLD,18)); center = new JPanel(); south = new JPanel();

hintButton = new JButton(\提示\); hintButton.addActionListener(this); showUsedTime = new JTextField(8); showUsedTime.setEditable(false); showIconNum = new JTextField(4); showIconNum.setEditable(false);

showIconNum.setHorizontalAlignment(JTextField.CENTER); showUsedTi …… 此处隐藏:2674字,全部文档内容请下载后查看。喜欢就下载吧 ……

《java程序大作业 - 记忆测试系统》 - 图文(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/565190.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)