記憶力測(cè)試
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.LinkedList;
public class Memory extends JFrame implements ActionListener
{
JMenuBar bar;
JMenu fileMenu;
JMenuItem 初級(jí),中級(jí),高級(jí),初級(jí)排行榜,中級(jí)排行榜,高級(jí)排行榜;
Block block[];
ImageIcon icon[];
MemoryTestPane 記憶測(cè)試板=null;
File file初級(jí)=new File("初級(jí)記憶排行榜.txt"),
file中級(jí)=new File("中級(jí)記憶排行榜.txt"),
file高級(jí)=new File("高級(jí)記憶排行榜.txt");
LinkedList 成績(jī)表=null;
ShowRecord 顯示成績(jī)表對(duì)話(huà)框=null;
int m=5,n=6;
int 圖標(biāo)個(gè)數(shù)=0;
Container con=null;
JTextField 提示條=null;
File gradeFile=null;
public Memory()
{
block=new Block[m*n];
圖標(biāo)個(gè)數(shù)=m;
icon=new ImageIcon[圖標(biāo)個(gè)數(shù)];
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].設(shè)置關(guān)閉時(shí)的圖標(biāo)(new ImageIcon("關(guān)閉.gif"));
}
bar=new JMenuBar();
fileMenu=new JMenu("記憶力測(cè)試");
初級(jí)=new JMenuItem("初級(jí)");
中級(jí)=new JMenuItem("中級(jí)");
高級(jí)=new JMenuItem("高級(jí)");
fileMenu.setMnemonic('G');
初級(jí).setMnemonic('B');
中級(jí).setMnemonic('I');
高級(jí).setMnemonic('E');
高級(jí)排行榜=new JMenuItem("高級(jí)排行榜");
中級(jí)排行榜=new JMenuItem("中級(jí)排行榜");
初級(jí)排行榜=new JMenuItem("初級(jí)排行榜");
初級(jí).setMnemonic('T');
fileMenu.add(初級(jí));
fileMenu.add(中級(jí));
fileMenu.add(高級(jí));
fileMenu.add(初級(jí)排行榜);
fileMenu.add(中級(jí)排行榜);
fileMenu.add(高級(jí)排行榜);
bar.add(fileMenu);
setJMenuBar(bar);
初級(jí).addActionListener(this);
中級(jí).addActionListener(this);
高級(jí).addActionListener(this);
初級(jí)排行榜.addActionListener(this);
中級(jí)排行榜.addActionListener(this);
高級(jí)排行榜.addActionListener(this);
成績(jī)表=new LinkedList();
if(!file初級(jí).exists())
{
try{
FileOutputStream out=new FileOutputStream("初級(jí)記憶排行榜.txt");
ObjectOutputStream object_out=new ObjectOutputStream(out);
object_out.writeObject(成績(jī)表);
object_out.close();
out.close();
}
catch(IOException e)
{
}
}
if(!file中級(jí).exists())
{
try{
FileOutputStream out=new FileOutputStream("中級(jí)記憶排行榜.txt");
ObjectOutputStream object_out=new ObjectOutputStream(out);
object_out.writeObject(成績(jī)表);
object_out.close();
out.close();
}
catch(IOException e)
{
}
}
if(!file高級(jí).exists())
{
try{
FileOutputStream out=new FileOutputStream("高級(jí)記憶排行榜.txt");
ObjectOutputStream object_out=new ObjectOutputStream(out);
object_out.writeObject(成績(jī)表);
object_out.close();
out.close();
}
catch(IOException e)
{
}
}
gradeFile=file初級(jí);
setBounds(100,100,300,260);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
con=getContentPane();
記憶測(cè)試板=new MemoryTestPane(block,icon,m,n,gradeFile);
提示條=new JTextField("初級(jí):您需要連續(xù)找出"+6+"個(gè)相同圖標(biāo)的方塊");
提示條.setEditable(false);
提示條.setForeground(Color.red);
con.add(記憶測(cè)試板,BorderLayout.CENTER);
con.add(提示條,BorderLayout.SOUTH);
con.validate();
this.validate();
}
public void 給出級(jí)別測(cè)試(int 寬,int 高,File f)
{
m=寬;
n=高;
圖標(biāo)個(gè)數(shù)=m;
gradeFile=f;
block=new Block[m*n];
icon=new ImageIcon[圖標(biāo)個(gè)數(shù)];
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].設(shè)置關(guān)閉時(shí)的圖標(biāo)(new ImageIcon("關(guān)閉.gif"));
}
記憶測(cè)試板=new MemoryTestPane(block,icon,m,n,gradeFile);
con.removeAll();
con.add(記憶測(cè)試板,BorderLayout.CENTER);
con.add(提示條,BorderLayout.SOUTH);
con.validate();
this.validate();
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==初級(jí))
{
給出級(jí)別測(cè)試(5,6,file初級(jí));
setBounds(100,100,300,260);
this.validate();
提示條.setText("初級(jí):您需要連續(xù)找出"+6+"個(gè)相同圖標(biāo)的方塊");
}
if(event.getSource()==中級(jí))
{
給出級(jí)別測(cè)試(6,7,file中級(jí));
setBounds(100,100,340,280);
this.validate();
提示條.setText("中級(jí):您需要連續(xù)找出"+7+"個(gè)相同圖標(biāo)的方塊");
}
if(event.getSource()==高級(jí))
{
給出級(jí)別測(cè)試(7,8,file高級(jí));
setBounds(100,100,360,300);
this.validate();
提示條.setText("高級(jí):您需要連續(xù)找出"+8+"個(gè)相同圖標(biāo)的方塊");
}
if(event.getSource()==高級(jí)排行榜)
{
顯示成績(jī)表對(duì)話(huà)框=new ShowRecord(this,file高級(jí));
顯示成績(jī)表對(duì)話(huà)框.setVisible(true);
}
if(event.getSource()==中級(jí)排行榜)
{
顯示成績(jī)表對(duì)話(huà)框=new ShowRecord(this,file中級(jí));
顯示成績(jī)表對(duì)話(huà)框.setVisible(true);
}
if(event.getSource()==初級(jí)排行榜)
{
顯示成績(jī)表對(duì)話(huà)框=new ShowRecord(this,file初級(jí));
顯示成績(jī)表對(duì)話(huà)框.setVisible(true);
}
}
public static void main(String args[])
{
new Memory();
}
}
posted @ 2005-12-18 10:44 luffy520 閱讀(585) | 評(píng)論 (0) | 編輯 收藏