Record.java
上传用户:penghbff
上传日期:2007-04-16
资源大小:32k
文件大小:3k
源码类别:

射击游戏

开发平台:

Java

  1. import java.io.*;
  2. import java.util.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import java.awt.*;
  6. public class Record extends JDialog implements ActionListener
  7. {
  8.   int time=0;
  9.   String grade=null;
  10.   String key=null;
  11.   String message=null;
  12.   Hashtable hashtable=null;
  13.   JTextField textName; 
  14.   JLabel label=null; 
  15.   JButton 确定,取消;
  16.   public Record(JFrame frame,Hashtable h)
  17.   {
  18.     super(frame,"记录你的成绩");
  19.     this.time=time; 
  20.     this.grade=grade;
  21.     hashtable=h;
  22.     setBounds(100,100,240,160);
  23.     setResizable(false);
  24.     setVisible(false);
  25.     setModal(true); 
  26.     确定=new JButton("确定");
  27.     取消=new JButton("取消");
  28.     textName=new JTextField(8);
  29.     textName.setText("匿名");
  30.     确定.addActionListener(this);
  31.     取消.addActionListener(this);
  32.     Container con=getContentPane();
  33.     con.setLayout(new GridLayout(2,1));
  34.     label=new JLabel("您现在是...高手,输入您的大名上榜");
  35.     con.add(label);
  36.     JPanel p=new JPanel();
  37.     p.add(textName);
  38.     p.add(确定);
  39.     p.add(取消);
  40.     con.add(p);
  41.     addWindowListener(new WindowAdapter()
  42.                         {
  43.                           public void windwoClosing(WindowEvent e)
  44.                             {
  45.                               setVisible(false);
  46.                               dispose();
  47.                             }
  48.                         }
  49.                      ); 
  50.   }
  51.   public void setGrade(String grade)
  52.   {
  53.     this.grade=grade;
  54.     label.setText("您现在是"+grade+"高手,输入您的大名上榜");
  55.   }
  56.   public void setTime(int time)
  57.   {
  58.     this.time=time;
  59.   }
  60.   public void actionPerformed(ActionEvent e)
  61.   { 
  62.     if(e.getSource()==确定)
  63.      {
  64.        message=grade+"#"+time+"#"+" "+textName.getText();
  65.        key=grade;
  66.        writeRecord(key,message);
  67.        setVisible(false);
  68.        dispose();
  69.      }
  70.     if(e.getSource()==取消)
  71.      {
  72.        setVisible(false);
  73.        dispose();
  74.      }  
  75.   }
  76.   public void  writeRecord(String key,String message)
  77.   {
  78.      File f=new File("英雄榜.txt");
  79.      try{
  80.           FileInputStream in=new FileInputStream(f);
  81.           ObjectInputStream object_in=new ObjectInputStream(in);
  82.           hashtable=(Hashtable)object_in.readObject();
  83.           object_in.close();
  84.           in.close(); 
  85.           String temp=(String)hashtable.get(key);
  86.           StringTokenizer fenxi=new StringTokenizer(temp,"#");
  87.           fenxi.nextToken();
  88.           int n=Integer.parseInt(fenxi.nextToken());
  89.           if(time<n) 
  90.               {
  91.                 hashtable.put(key,message);
  92.                 FileOutputStream out=new FileOutputStream(f);
  93.                 ObjectOutputStream object_out=new ObjectOutputStream(out);
  94.                 object_out.writeObject(hashtable);
  95.                 object_out.close();
  96.                 out.close();
  97.               }
  98.          }
  99.      catch(Exception e) 
  100.          {
  101.             System.out.println(e);
  102.          }
  103.    }
  104. }