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 初級,中級,高級,初級排行榜,中級排行榜,高級排行榜;
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("記憶力測試");
初級=new JMenuItem("初級");
中級=new JMenuItem("中級");
高級=new JMenuItem("高級");
fileMenu.setMnemonic('G');
初級.setMnemonic('B');
中級.setMnemonic('I');
高級.setMnemonic('E');
高級排行榜=new JMenuItem("高級排行榜");
中級排行榜=new JMenuItem("中級排行榜");
初級排行榜=new JMenuItem("初級排行榜");
初級.setMnemonic('T');
fileMenu.add(初級);
fileMenu.add(中級);
fileMenu.add(高級);
fileMenu.add(初級排行榜);
fileMenu.add(中級排行榜);
fileMenu.add(高級排行榜);
bar.add(fileMenu);
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();
}
}