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

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

来源:网络收集 时间:2026-05-31
导读: ShowRecordDialog()是构造方法,负责创新showDialog对象。 setGradeFile(File)方法。ShowRecordDialog类创建的ShowrecordDialog对话框是主类MemoryGame窗口中的一个成员。当用户选择窗口上的“查看排行榜”菜单

ShowRecordDialog()是构造方法,负责创新showDialog对象。

setGradeFile(File)方法。ShowRecordDialog类创建的ShowrecordDialog对话框是主类MemoryGame窗口中的一个成员。当用户选择窗口上的“查看排行榜”菜单中的菜单项时,ShowrecordDialog对话框调用setGradeFile(File)方法将相应的级别文件传递给gradeFile。

showRecord()方法。ShowrecordDialog对话框调用该方法读取gradeFile文件中的成绩,为了将成绩按高低顺序显示在showArea文本区中,showRecord()方法跟读取的名字和该名字的对应成绩,创建一个People对象,并将该People对象存放在treeSet树集中。

actionPerformed(ActionEvent)是ActionListener接口中的方法,clear注册了ActionEvent事件,当用户单击clear按钮时,actionPerformed(ActionEvent)方法被调用执行,所执行的操作是清除gradeFile文件中的内容。

3. 代码(ShowRecordDialog)

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

public class ShowRecordDialog extends JDialog implements ActionListener{

File gradeFile; JButton clear;

JTextArea showArea = null; TreeSettreeSet; public ShowRecordDialog(){ }

21 / 30

treeSet = new TreeSet(); showArea = new JTextArea(6,6);

showArea.setFont(new Font(\楷体\,Font.BOLD,20)); clear = new JButton(\清空排行榜\); clear.addActionListener(this);

add(new JScrollPane(showArea),BorderLayout.CENTER); add(clear,BorderLayout.SOUTH); setBounds(250,250,500,250); setModal(true);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){ }

setVisible(false);

});

}

public void setGradeFile(File f){ }

public void showRecord(){ }

public void actionPerformed(ActionEvent e) { }

22 / 30

gradeFile = f;

setTitle(f.getName());

showArea.setText(null); treeSet.clear(); try{ }

catch(IOException exp) {System.out.println(exp);}

RandomAccessFile in = new RandomAccessFile(gradeFile,\); long fileLength = in.length(); long readPosition = 0;

while (readPosition < fileLength){ }

in.close();

Iteratoriter = treeSet.iterator(); while(iter.hasNext()){ }

People p = iter.next();

showArea.append(\姓名: \ + p.getName()+ \,成绩 :\ + p.getTime() + \秒\ + \,//showArea.append(\姓名: \,成绩 :\秒\ showArea.append(\); String name = in.readUTF(); int time = in.readInt(); int iconNum = in.readInt();

readPosition = in.getFilePointer();

People people = new People(name,time,iconNum); treeSet.add(people);

鼠标单击次数:\ + p.getIconNum());

if (e.getSource() == clear) { }

try { }

catch(Exception ee){}

File f = gradeFile.getAbsoluteFile(); gradeFile.delete(); f.createNewFile();

showArea.setText(\排行榜被清空\);

1.3.5 People类

1. 效果图 People是我们编写的一个类,该类对象中的数据是用户的姓名和成绩,被显示在ShowRecordDialog对话框中的showArea文本区中(见下图)。

2. UML图 People对象中的数据由ShowRecordDialog对话框从gradeFile文件中读取的用户姓名和成绩所构成。ShowRecordDialog对话框将People对象作为其treeSet树集上的节点,以便按着成绩高低顺序排列People对象。标明People类的主要成员变量、方法以及和ShowrecordDialog类之间关系的UML图如下图 People ShowRecordDialog

name:String treeSet:TreeSet time:int

iconNum:int

getTime():int

getName():String

getIconNum():int

compareTo(Object):int

People类的UML图 以下是UML图中有关数据和方法的详细说明。 1) 成员变量 Name是用户的名字。 Time是用户的用时。

IconNum是用户鼠标单击次数。 2) 方法

23 / 30

getTime()方法返回time。 getName()方法返回name。 getIconNum()方法返回iconNum。

compareTo(Object)是Compareable接口中的方法,其操作是确定People对象的大小关系。 3. 代码(People.java)

import java.io.*;

public class People implements Serializable,Comparable{

String name = null; int iconNum = 0; int time = 0;

public People (String name,int t,int n) { }

//public People(String name2, int time2, int iconNum) {

// TODO Auto-generated constructor stub //}

public int getTime() { }

public String getName(){ }

public int getIconNum(){ } */

if ((this.time+this.iconNum)>=(p.time+p.iconNum))

return 1;

return ((this.time + this.iconNum)-(p.time + p.iconNum));

24 / 30

this.name = name; time = t; iconNum = n;

return time;

return name;

return iconNum;

public int compareTo(Object b) {

People p = (People) b;

return 1;

return (this.time-p.time);

//时间与点击鼠标次数占分数的

/* if ((this.time-p.time) == 0)

else

权重相等,时间与鼠标单击次数之和越小,成绩越靠前!

else

//这句其实不太明

白,效仿上面 “等价”代换而已,而且所期望的结果也能实现!

}

}

1.3.6 Record类

1. 效果图 Record创建的对话框如下图

2. UML图 Record是javax.swing包中JDialog的子类,并实现了ActionListener接口,该类创建的对象record是MemoryTestArea类的成员之一。当用户成功单击出相应级别所要求的图标相同的方块后,程序弹出Record对话框,用户使用对话框提供的界面将成绩保存到相应的级别文件中。标明了Record类的主要成员变量、方法以及和MemoryTestArea类之间关系的UML图如下图所示。

JDialog ActionListener

Record MemoryTestAea time:int enter,cancel:JButton record:Recor …… 此处隐藏:2221字,全部文档内容请下载后查看。喜欢就下载吧 ……

《java程序大作业 - 记忆测试系统》 - 图文(7).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)