




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/主类MemoryGameimport javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.LinkedList;public class MemoryGame extends JFrame implements ActionListener JMenuBar bar; JMenu menuGrade,menuResult,menuIcon; JMenuItem oneGradeItem,twoGradeItem,threeGradeItem; JMenuItem oneGradeResult,twoGradeResult,threeGradeResult; JMenuItem carImageIcon,animalImageIcon; File fileOneGrade,fileTwoGrade,fileThreeGrade,gradeFile; String imageName; MemoryTestArea memoryArea; ShowRecordDialog showDiolag=null; int m=5,n=6; final int imageNumber=7; MemoryGame() fileOneGrade=new File(初级记忆排行榜.txt); fileTwoGrade=new File(中级记忆排行榜.txt); fileThreeGrade=new File(高级记忆排行榜.txt); bar=new JMenuBar(); menuGrade=new JMenu(选择级别); oneGradeItem=new JMenuItem(初级); twoGradeItem=new JMenuItem(中级); threeGradeItem=new JMenuItem(高级); menuGrade.add(oneGradeItem); menuGrade.add(twoGradeItem); menuGrade.add(threeGradeItem); menuResult=new JMenu(查看排行榜); oneGradeResult=new JMenuItem(初级排行榜); twoGradeResult=new JMenuItem(中级排行榜); threeGradeResult=new JMenuItem(高级排行榜); menuResult.add(oneGradeResult); menuResult.add(twoGradeResult); menuResult.add(threeGradeResult); menuIcon=new JMenu(选择图标); carImageIcon=new JMenuItem(汽车图标); animalImageIcon=new JMenuItem(动物图标); animalImageIcon.addActionListener(this); carImageIcon.addActionListener(this); menuIcon.add(carImageIcon); menuIcon.add(animalImageIcon); bar.add(menuGrade); bar.add(menuResult); bar.add(menuIcon); setJMenuBar(bar); oneGradeItem.addActionListener(this); twoGradeItem.addActionListener(this); threeGradeItem.addActionListener(this); oneGradeResult.addActionListener(this); twoGradeResult.addActionListener(this); threeGradeResult.addActionListener(this); if(!fileOneGrade.exists() try fileOneGrade.createNewFile(); catch(IOException exp) if(!fileTwoGrade.exists() try fileTwoGrade.createNewFile(); catch(IOException exp) if(!fileThreeGrade.exists() try fileThreeGrade.createNewFile(); catch(IOException exp) setBounds(100,100,400,360); setVisible(true); memoryArea=new MemoryTestArea(); imageName=new StringimageNumber; for(int i=0;iimageName.length;i+) imageNamei=new String(car+i+.jpg); m=5; n=6; gradeFile=fileOneGrade; memoryArea.initBlock(m,n,imageName,gradeFile); add(memoryArea,BorderLayout.CENTER); showDiolag=new ShowRecordDialog(); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void actionPerformed(ActionEvent event) if(event.getSource()=oneGradeItem) m=5; n=6; gradeFile=fileOneGrade; memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=twoGradeItem) m=6; n=7; gradeFile=fileTwoGrade; memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=threeGradeItem) m=7; n=8; gradeFile=fileThreeGrade; memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=carImageIcon) for(int i=0;iimageName.length;i+) imageNamei=new String(car+i+.jpg); memoryArea.setImageName(imageName); memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=animalImageIcon) for(int i=0;iimageName.length;i+) imageNamei=new String(ani+i+.jpg); memoryArea.setImageName(imageName); memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=oneGradeResult) showDiolag.setGradeFile(fileOneGrade); showDiolag.showRecord(); showDiolag.setVisible(true); if(event.getSource()=twoGradeResult) showDiolag.setGradeFile(fileTwoGrade); showDiolag.showRecord(); showDiolag.setVisible(true); if(event.getSource()=threeGradeResult) showDiolag.setGradeFile(fileThreeGrade); showDiolag.showRecord(); showDiolag.setVisible(true); public static void main(String args) new MemoryGame(); /MemoryTestArea类import javax.swing.*; import java.awt.event.*;import java.awt.*;import java.util.*;import java.io.*;public class MemoryTestArea extends JPanel implements ActionListener,Runnable int row,col; File gradeFile; ArrayList allBlockList; String imageFileName; LinkedList openIconList; LinkedList openBlockList; int success=0; Thread hintThead; JButton hintButton; int usedTime=0; JTextField showUsedTime,hintMessage; javax.swing.Timer timer; Record record; JPanel center,south; MemoryTestArea() setLayout(new BorderLayout(); allBlockList=new ArrayList(); openIconList=new LinkedList(); openBlockList=new LinkedList(); hintThead=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); showUsedTime.setHorizontalAlignment(JTextField.CENTER); south.add(new JLabel(用时:) ; south.add(showUsedTime); south.add(new JLabel(提示图标位置(导致用时增加):) ; south.add(hintButton); add(south,BorderLayout.SOUTH); add(hintMessage,BorderLayout.NORTH); timer=new javax.swing.Timer(1000,this); record=new Record(); public void initBlock(int m,int n,String name,File f) row=m; col=n; gradeFile=f; center.removeAll(); imageFileName=name; ImageIcon icon=new ImageIconimageFileName.length; for(int i=0;iicon.length;i+) iconi=new ImageIcon(imageFileNamei); if(allBlockList.isEmpty() for(int i=0;irow*col;i+) allBlockList.add(new Block(); else allBlockList.clear(); for(int i=0;irow*col;i+) allBlockList.add(new Block(); for(int i=0;iallBlockList.size();i+) allBlockList.get(i).addActionListener(this); allBlockList.get(i).setOpenStateIcon(iconi%row); Collections.shuffle(allBlockList); center.setLayout(new GridLayout(row,col); for(int i=0;iallBlockList.size();i+) center.add(allBlockList.get(i); add(center,BorderLayout.CENTER); if(timer.isRunning() timer.stop(); hintMessage.setText(您需要用鼠标单击出+col+个同样图标的方块); usedTime=0; showUsedTime.setText(null); validate(); public void setImageName(String name) imageFileName=name; public void actionPerformed(ActionEvent e) if(e.getSource() instanceof Block) if(!timer.isRunning() timer.start(); Block block=(Block)e.getSource(); ImageIcon openStateIcon=block.getOpenStateIcon(); block.setIcon(openStateIcon); if(openIconList.size()=0) openIconList.add(openStateIcon); openBlockList.add(block); success=1; else ImageIcon temp=openIconList.getLast(); if(temp=openStateIcon&!(openBlockList.contains(block) success=success+1; openIconList.add(openStateIcon); openBlockList.add(block); if(success=col) for(int i=0;iallBlockList.size();i+) allBlockList.get(i).setEnabled(false); for(int j=0;jopenBlockList.size();j+) Block b=openBlockList.get(j); b.setDisabledIcon(b.getOpenStateIcon(); timer.stop(); record.setTime(usedTime); record.setGradeFile(gradeFile); record.setVisible(true); else if(temp!=openStateIcon)&(!(openBlockList.contains(block) openIconList.clear(); openBlockList.clear(); openIconList.add(openStateIcon); openBlockList.add(block); success=1; for(int i=0;iallBlockList.size();i+) if(allBlockList.get(i)!=block) allBlockList.get(i).setIcon(null); if(e.getSource()=hintButton) if(!hintThead.isAlive() hintThead=new Thread(this); for(int i=0;iallBlockList.size();i+) allBlockList.get(i).removeActionListener(this); usedTime=usedTime+10; try hintThead.start(); catch(IllegalThreadStateException ex) if(e.getSource()=timer) usedTime+; showUsedTime.setText(您的用时:+usedTime+秒); public void run() for(int i=0;iallBlockList.size();i+) allBlockList.get(i).setIcon(allBlockList.get(i).getOpenStateIcon(); try Thread.sleep(1200); catch(InterruptedException exp) for(int i=0;iallBlockList.size();i+) allBlockList.get(i).addActionListener(this); for(int i=0;iallBlockList.size();i+) if(!openBlockList.contains(allBlockList.get(i) allBlockList.get(i).setIcon(null); /Block 类import javax.swing.*;import java.awt.event.*;public class Block extends JButton ImageIcon openStateIcon; public ImageIcon getOpenStateIcon() return openStateIcon; public void setOpenStateIcon(ImageIcon icon) openStateIcon=icon; /Record类import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class Record extends JDialog implements ActionListener int time=0; JTextField yourName; JLabel label; JButton enter,cancel; File gradeFile=null; public Record() setBounds(100,100,330,160); setResizable(false); setModal(true); setVisible(false); enter=new JButton(确定); cancel=new JButton(取消); yourName=new JTextField(8); yourName.setText(匿名); enter.addActionListener(this); cancel.addActionListener(this); setLayout(new GridLayout(2,1); label=new JLabel(); add(label); JPanel p=new JPanel(); p.add(yourName); p.add(enter); p.add(cancel); add(p); public void setGradeFile(File f) gradeFile=f; setTitle(保存成绩到+gradeFile.getName(); label.setText(保存成绩到+gradeFile.getName(); validate(); public void setTime(int time) this.time=time; public void actionPerformed(ActionEvent e) if(e.getSource()=enter) LinkedList list=new LinkedList(); try RandomAccessFile out=new RandomAccessFile(gradeFile,rw); out.seek(out.length(); out.writeUTF(yourName.getText(); out.writeInt(time); out.close(); catch(Exception event) setVisible(false); if(e.getSource()=cancel) setVisible(false); /ShowRecordDialog 类import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class ShowRecordDialog extends JDialog implements ActionListener File gradeFile; JButton clear; JTextArea showArea=null; TreeSet treeSet; public ShowRecordDialog() treeSet=new TreeSet(); showArea=new JTextArea(6,4); showArea.setFont(new Font(楷体,Font.BOLD,20); clear=new JButton(清空排行榜); clear.addActionListener(this); add(new JScrollPane(showArea),BorderLayout.CENTER); add(clear,BorderLayout.SOUTH); setBounds(100,100,320,185); setModal(true); addWindowListener(new WindowAdapter() public void windwoClosing(WindowEvent e) setVisible(false); ); public void setGradeFile(File f) gradeFile=f; setTitle(f.getName(); public void showRecord() showArea.setText(null); treeSet.clear(); try RandomAccessFile in=new RandomAccessFile(gradeFile,rw); long fileLength=in.length(); long readPosition=0; while(readPositionfileLength) String name=in.readUTF(); int time=in.readInt(); readPosition=in.getFilePointer(); People people=new People(name,time); treeSet.add(people); in.close(); Iterator iter=treeSet.iterator(); while(iter.hasNext() People p=iter.next(); showArea.append(姓名:+p.getName()+,成绩: +p.getTime()+秒); showArea.append(n); catch(IOException exp)System.out.println(exp); public void actionPerformed(ActionEvent e) if(e.getSource()=clear) try File f=gradeFile.getAbsoluteFile(); gradeFile.delete(); f.createNewFile(); showArea.setText(排行榜被清空); catch(Exception ee) /ShowRecord类import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class ShowRecord extends JDialog implements ActionListener File gradeFile; JButton clear; JTextArea showArea=null; People people; TreeSet treeSet; public ShowRecord(File f) treeSet=new TreeSet(); showArea=new JTextArea(6,4); clear=new JButton(清空排行榜); clear.addActionListener(this); add(new JScrollPane(showArea),BorderLayout.CENTER); add(clear,BorderLayout.SOUTH); setBounds(100,100,320,185)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 风险应对策略的选择试题及答案
- 高考语文考场应用试题及答案
- 湖北省咸宁市咸安区2025年八下数学期末统考试题含解析
- 制定个人学习与发展路径计划
- 细分市场的品牌定位研究计划
- 提升领导力的实践方法计划
- 计算机科学专业进阶学习策略试题及答案
- 计算机辅助翻译(CAT)软件应用试题及答案
- 2024年陕西科技大学辅导员考试真题
- 风险管理中的人才培养与发展试题及答案
- 公司师徒制、导师制管理办法(完整版方案)
- 解剖学公开课课件内分泌
- 家族财富管理
- 高中必修一英语单词湘教版
- 森林防火预警监测
- 银屑病临床病例讨论
- 涉密人员审查备案登记表
- 园林绿化员工安全培训
- 蛙泳教学课件教学课件
- 高层建筑汽车吊吊装作业方案
- 【初中历史】大一统王朝的巩固+课件-2024-2025学年统编版(2024)七年级历史上
评论
0/150
提交评论