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

📄 memory.java

📁 实现简单的记忆功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          listBlock.add(方块);
          success=1;                                            
         }
        else
         {
           ImageIcon temp=(ImageIcon)listIocn.getLast();        
           if(temp==翻开时的图标&&!(listBlock.contains(方块)))  
              {
                success=success+1;                              
                listIocn.add(翻开时的图标);                     
                listBlock.add(方块);
                if(success==列数) 
                  {
                    for(int i=0;i<block.length;i++)           
                     {
                       block[i].setEnabled(false);
                     }
                    for(int j=0;j<listBlock.size();j++)
                     {
                       Block b=(Block)listBlock.get(j);
                       b.setDisabledIcon(b.获取翻开时的图标());
                     } 
                    计时器.stop();
                    Record record=new Record(gradeFile);
                    record.setTime(time);
                    record.setVisible(true);
                  }    
               }
           else if((temp!=翻开时的图标)&&(!(listBlock.contains(方块)))) 
               {
                listIocn.clear();                          
                listBlock.clear();
                listIocn.add(翻开时的图标);                
                listBlock.add(方块);
                success=1;                                 
                for(int i=0;i<block.length;i++)            
                  {
                    if(方块!=block[i])
                       {
                         block[i].设置图标(block[i].获取关闭时的图标());
                       }
                 }                
               }  
         }
       
       if(计时器是否启动==false)
         {
          time=0;
          计时器.start();                                   
          计时器是否启动=true;                              
         }
    }
   if(e.getSource()==计时器)
    {
      time=time+1;
      显示时间.setText("您的用时:"+time+"秒");
    }
     
 }
}



class People implements java.io.Serializable
{
  String name=null;
  int time=0;                          
 public People(String name,int t)
  {
    this.name=name;
    time=t;
  }
 public int getTime()
  {
    return time;
  }
 public String getName()
  {
   return name;
  }
}


class Point
{ 
  int x;    
  Point(int i)
  {
    x=i;
  }
  public int getInt()
  {
    return x;
  }
}


 class  RandomSetIcon 
 {
  public RandomSetIcon()
    {
    }
  public void 随机设置图标(Block[] block,ImageIcon icon[])
    {
    	
      Vector vector=new Vector();                   
      int n=icon.length;
     for(int i=0;i<block.length;i++)                
       { 
         int x=i%n;
         Point p=new Point(x);
        vector.addElement(p);
       } 
     for(int i=0;i<block.length;i++)
       {
         int m=(int)(Math.random()*vector.size()); 
         Point p=(Point)vector.elementAt(m);   
         int x=p.getInt();                         
         block[i].设置翻开时的图标(icon[x]); 
         vector.remove(m);           
       }
    } 
 } 


class Record extends JFrame implements ActionListener
{
  int time=0;
  JTextField yourName,label; 
  JButton 确定,取消;
  File gradeFile=null;
  public Record(File f)
  {
    super("记录你的成绩到:"+f.toString());
    gradeFile=f;
    setBounds(100,100,330,160);
    setResizable(false);
    setVisible(false);
    确定=new JButton("确定");
    取消=new JButton("取消");
    yourName=new JTextField(8);
    yourName.setText("匿名");
    确定.addActionListener(this);
    取消.addActionListener(this);
    Container con=getContentPane();
    con.setLayout(new GridLayout(2,1));
    label=new JTextField("输入你的姓名,将成绩存放到排行榜:"+f.toString());
    label.setEditable(false);
    con.add(label);
    JPanel p=new JPanel();
    p.add(yourName);
    p.add(确定);
    p.add(取消);
    con.add(p);
    addWindowListener(new WindowAdapter()
                        {
                          public void windwoClosing(WindowEvent e)
                            {
                              setVisible(false);
                              dispose();
                            }
                        }
                     ); 
  }
  public void setFile(File f)
  { 
    gradeFile=f;
  }
  public void setTime(int time)
  {
    this.time=time;
  }
  public void actionPerformed(ActionEvent e)
  { 
    if(e.getSource()==确定)
     {
      try{
          FileInputStream in=new FileInputStream(gradeFile);
          ObjectInputStream object_in=new ObjectInputStream(in);
          LinkedList list=(LinkedList)object_in.readObject();
          object_in.close();
          in.close(); 
          People people=new People(yourName.getText(),time);
          list.add(people);
          FileOutputStream out=new FileOutputStream(gradeFile);
          ObjectOutputStream object_out=new ObjectOutputStream(out);
          object_out.writeObject(list);
          out.close();
          object_out.close();
           
         }
     catch(Exception event) 
         {
            System.out.println(event);
         }
       
       setVisible(false);
       dispose();
     }
    if(e.getSource()==取消)
     {
       setVisible(false);
       dispose();
     }  
  }
}





class ShowRecord extends JDialog implements ActionListener
{ 
  File gradeFile=null;
  JButton 确定,清除;
  JTextArea show=null;
 public ShowRecord(JFrame frame,File f)
  {
    super(frame,"记忆测试排行榜:"+f.toString());
    gradeFile=f;
    show=new JTextArea(6,4);
    确定=new JButton("显示排行榜");
    确定.addActionListener(this);
    清除=new JButton("清空排行榜");
    清除.addActionListener(this);
    Container con=getContentPane(); 
    con.add(new JScrollPane(show),BorderLayout.CENTER);
    JPanel p=new JPanel();
    p.add(确定);
    p.add(清除);
    con.add(p,BorderLayout.SOUTH);
    setBounds(100,100,320,185);
    setVisible(false);
    setModal(true); 
   
    addWindowListener(new WindowAdapter()
                        {
                          public void windwoClosing(WindowEvent e)
                            {
                              setVisible(false);
                              dispose();
                            }
                        }
                     ); 
   }

 public void actionPerformed(ActionEvent e)
  { 
    
    if(e.getSource()==确定)
     { 
       try
         {
           show.setText(null);
           FileInputStream in=new FileInputStream(gradeFile);
           ObjectInputStream object_in=new ObjectInputStream(in);
           LinkedList 成绩=(LinkedList)object_in.readObject();
           object_in.close();
           sort(成绩);                                        
           for(int i=0;i<成绩.size();i++)
            {
              People people=(People)成绩.get(i);
              show.append("\n"+people.getName()+"成绩:"+people.getTime());
            }
         }
        catch(Exception ee)
         {
         }
     }
   if(e.getSource()==清除)
     { 
       try
         {
           FileInputStream in=new FileInputStream(gradeFile);
           ObjectInputStream object_in=new ObjectInputStream(in);
           LinkedList 成绩=(LinkedList)object_in.readObject();
           object_in.close();
           成绩.clear();
           FileOutputStream out=new FileOutputStream(gradeFile);
           ObjectOutputStream object_out=new ObjectOutputStream(out);
           object_out.writeObject(成绩);
           out.close();
           object_out.close(); 
           show.setText("排行榜被清空");
         }
        catch(Exception ee)
         {
         }
     }    
  }
 public void sort(LinkedList list)
  {
    for(int i=0;i<list.size()-1;i++)
      {
        
         for(int j=i+1;j<list.size();j++)
            {
             if(((People)list.get(i)).getTime()>((People)list.get(j)).getTime())
                {
                   People temp=(People)list.get(j);
                   list.set(j,(People)list.get(i));
                   list.set(i,temp);
                }
            } 
      }
  }
}

⌨️ 快捷键说明

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