⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memory.java

📁 实现简单的记忆功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.LinkedList;
import java.util.Vector;
public class Memory extends JFrame implements ActionListener
{ 
  JMenuBar bar;
  JMenu fileMenu;
  JMenuItem 初级,中级,高级,初级排行榜,中级排行榜,高级排行榜,帮助主题,关于记忆测试面板,剪切,复制,粘贴;
  Block block[];
  ImageIcon icon[];
  MemoryTestPane 记忆测试板=null;
  File file初级=new File("初级记忆排行榜.txt"),
       file中级=new File("中级记忆排行榜.txt"),
       file高级=new File("高级记忆排行榜.txt");

  LinkedList 成绩表=null;
  ShowRecord 显示成绩表对话框=null;
  int m=5,n=6;                                        
  int 图标个数=0;
  Container con=null;
  JTextField 提示条=null;
  File gradeFile=null;            
  public  Memory()
  { 
    block=new Block[m*n];
    图标个数=m; 
    icon=new ImageIcon[图标个数]; 
    for(int i=0;i<icon.length;i++)
       {
         icon[i]=new ImageIcon("a"+i+".gif");
       } 
    for(int i=0;i<block.length;i++)
       {
         block[i]=new Block();
         block[i].设置关闭时的图标(new ImageIcon("关闭.gif"));
       }
  
   bar=new JMenuBar();
    fileMenu=new JMenu("记忆力测试");
    fileMenu.setForeground(Color.blue);
    初级=new JMenuItem("初级");
     初级.setForeground(Color.yellow);
    中级=new JMenuItem("中级");
     中级.setForeground(Color.yellow);
    高级=new JMenuItem("高级");
    高级.setForeground(Color.yellow);
    fileMenu.setMnemonic('G');
    初级.setMnemonic('B');
    中级.setMnemonic('I');
    高级.setMnemonic('E');
     高级排行榜=new JMenuItem("高级排行榜");
    高级排行榜.setForeground(Color.green);
    中级排行榜=new JMenuItem("中级排行榜");
     中级排行榜.setForeground(Color.green);
    初级排行榜=new JMenuItem("初级排行榜");
     初级排行榜.setForeground(Color.green);
    初级.setMnemonic('T');
    fileMenu.add(初级);
    fileMenu.add(中级);
    fileMenu.add(高级);
    fileMenu.add(初级排行榜);
    fileMenu.add(中级排行榜);
    fileMenu.add(高级排行榜);
    bar.add(fileMenu);
    fileMenu = new JMenu("编辑(E)");
    bar.add(fileMenu);
    剪切=new JMenuItem("剪切(T)  Ctrl+T");    
    fileMenu.add("剪切(T)  Ctrl+T");
    复制=new JMenuItem("复制(C)  Ctrl+C");
    fileMenu.add("复制(C)  Ctrl+C");
    粘贴=new JMenuItem("粘贴(P)  Ctrl+P");
    fileMenu.add("粘贴(P)  Ctrl+P");
    fileMenu = new JMenu("帮助(H)");
    bar.add(fileMenu);
    帮助主题=new JMenuItem("帮助主题(H)");
    fileMenu.add("帮助主题(H)");
    关于记忆测试面板=new JMenuItem("关于记忆测试面板(A)");
    fileMenu.add("关于记忆测试面板(A)");
    setJMenuBar(bar);
    初级.addActionListener(this);
    中级.addActionListener(this);
    高级.addActionListener(this);
    初级排行榜.addActionListener(this);
    中级排行榜.addActionListener(this);
    高级排行榜.addActionListener(this);
    成绩表=new LinkedList();
    if(!file初级.exists())                         
     {
      try{
          FileOutputStream out=new FileOutputStream("初级记忆排行榜.txt");
          ObjectOutputStream object_out=new ObjectOutputStream(out);
          object_out.writeObject(成绩表);
          object_out.close();
          out.close();
         }
      catch(IOException e)
         {
         }
     } 
     if(!file中级.exists())
     {
      try{
          FileOutputStream out=new FileOutputStream("中级记忆排行榜.txt");
          ObjectOutputStream object_out=new ObjectOutputStream(out);
          object_out.writeObject(成绩表);
          object_out.close();
          out.close();
         }
      catch(IOException e)
         {
         }
     } 
     if(!file高级.exists())
     {
      try{
          FileOutputStream out=new FileOutputStream("高级记忆排行榜.txt");
          ObjectOutputStream object_out=new ObjectOutputStream(out);
          object_out.writeObject(成绩表);
          object_out.close();
          out.close();
         }
      catch(IOException e)
         {
         }
     } 
    gradeFile=file初级;
    setBounds(100,100,300,260);
    setVisible(true);
    addWindowListener(new WindowAdapter()
      {
        public void windowClosing(WindowEvent e)
         {
          System.exit(0);
         }
      });
    con=getContentPane(); 
    记忆测试板=new MemoryTestPane(block,icon,m,n,gradeFile);
    提示条=new JTextField("初级:您需要连续找出"+6+"个相同图标的方块"); 
    提示条.setEditable(false);
    提示条.setForeground(Color.red);
    con.add(记忆测试板,BorderLayout.CENTER);
    con.add(提示条,BorderLayout.SOUTH);
    con.validate();
    this.validate();
 }
 public void 给出级别测试(int 宽,int 高,File f)
 {    
    m=宽;
    n=高;
    图标个数=m;
    gradeFile=f;
    block=new Block[m*n];
    icon=new ImageIcon[图标个数];
       for(int i=0;i<icon.length;i++)
       {
         icon[i]=new ImageIcon("a"+i+".gif");
       } 
    for(int i=0;i<block.length;i++)
       {
         block[i]=new Block();
         block[i].设置关闭时的图标(new ImageIcon("关闭.gif"));
       }
    记忆测试板=new MemoryTestPane(block,icon,m,n,gradeFile);
    con.removeAll();
    con.add(记忆测试板,BorderLayout.CENTER);
    con.add(提示条,BorderLayout.SOUTH);
    con.validate();
    this.validate();
 }
 public void actionPerformed(ActionEvent event)
 { 
   if(event.getSource()==初级) 
       {  
         给出级别测试(5,6,file初级);
         setBounds(100,100,300,260);
         this.validate();
         提示条.setText("初级:您需要连续找出"+6+"个相同图标的方块");
       }
   if(event.getSource()==中级) 
       {
         给出级别测试(6,7,file中级);
         setBounds(100,100,340,280);
         this.validate();
         提示条.setText("中级:您需要连续找出"+7+"个相同图标的方块");
       }
   if(event.getSource()==高级) 
       {  
         给出级别测试(7,8,file高级);
         setBounds(100,100,360,300);
         this.validate();
         提示条.setText("高级:您需要连续找出"+8+"个相同图标的方块");
       }
   if(event.getSource()==高级排行榜)
       {
         显示成绩表对话框=new ShowRecord(this,file高级);
         显示成绩表对话框.setVisible(true);
       }
    if(event.getSource()==中级排行榜)
       {
         显示成绩表对话框=new ShowRecord(this,file中级);
         显示成绩表对话框.setVisible(true);
       }
     if(event.getSource()==初级排行榜)
       {
         显示成绩表对话框=new ShowRecord(this,file初级);
         显示成绩表对话框.setVisible(true);
       }
 } 

 public static void main(String args[])
  {
    new Memory();
  }
}




class Block extends JButton implements ActionListener
{
 ImageIcon 翻开时的图标=null,关闭时的图标=null;
 public Block()
  {
    addActionListener(this);
  } 
 public ImageIcon 获取翻开时的图标()
  {
    return 翻开时的图标;
  }  
 public ImageIcon 获取关闭时的图标()
  {
    return 关闭时的图标;
  }  
 public void 设置翻开时的图标(ImageIcon icon)
  {
    翻开时的图标=icon;
  }
 public void 设置关闭时的图标(ImageIcon icon)
  {
    关闭时的图标=icon;
  }
 public void 设置图标(ImageIcon icon)
  {
    setIcon(icon);    
  }
 public void actionPerformed(ActionEvent e)
  {
    this.setIcon(翻开时的图标);
  }
}


class MemoryTestPane extends JPanel implements ActionListener
{
  RandomSetIcon  排列图标=null;
  Block block[];                         
  ImageIcon icon[];                      
  LinkedList  listIocn=null,             
              listBlock=null;            
  int 行数=0,列数=0;
  int success=0;                         
                                         
  int time=0;                            
  javax.swing.Timer 计时器=null;         
  JTextField 显示时间=null;
  File gradeFile=null;
  boolean 计时器是否启动=false;
  public  MemoryTestPane(Block[] block,ImageIcon[] icon,int m,int n,File f)
   {
     排列图标=new RandomSetIcon();
     this.block=block;
     this.icon=icon;
     行数=m;
     列数=n;
     gradeFile=f;
     listIocn=new LinkedList();
     listBlock=new LinkedList();
     setLayout(new BorderLayout());
     JPanel center=new JPanel();
     center.setLayout(new GridLayout(行数,列数));
     for(int i=0;i<block.length;i++)
        {
          center.add(block[i]);
          block[i].addActionListener(this);
        }
     JPanel south=new JPanel();
     显示时间=new JTextField(12);
     显示时间.setEditable(false);
     显示时间.setForeground(Color.red);
     south.add(显示时间);
     add(center,BorderLayout.CENTER);
     add(south,BorderLayout.SOUTH); 
     排列图标.随机设置图标(block,icon);
     计时器=new Timer(1000,this);                              
     计时器是否启动=false;
   }
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource() instanceof Block)
    { 
        Block 方块=(Block)e.getSource();
        ImageIcon 翻开时的图标=方块.获取翻开时的图标();
        方块.设置图标(翻开时的图标);
        if(listIocn.size()==0)                                  
         {
          listIocn.add(翻开时的图标);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -