1、记忆测试系统 JAVA实现/主类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
2、; 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=
3、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);
4、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(选择图标); car
5、ImageIcon=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.add
6、ActionListener(this); twoGradeItem.addActionListener(this); threeGradeItem.addActionListener(this); oneGradeResult.addActionListener(this); twoGradeResult.addActionListener(this); threeGradeResult.addActionListener(this); if(!fileOneGrade.exists() try fileOneGrade.createNewFile(); catch(IOException
7、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;i
8、imageName.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(Act
9、ionEvent 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; grade
10、File=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
11、 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()=twoG
12、radeResult) 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(); /Memor
13、yTestArea类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 o
14、penBlockList; 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(); openBlockLi
15、st=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.addActi
16、onListener(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(hintMessag
17、e,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 ImageI
18、con(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).setOpenStateIc
19、on(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.s
20、etText(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(ope
21、nStateIcon); 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)
22、 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!=open
23、StateIcon)&(!(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(!hintT
24、head.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 ru
25、n() 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(!openBlockLis
26、t.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
27、; /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,1
28、60); 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 JPa
29、nel(); 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
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1