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

📄 fileoperator.java

📁 一个JAVA小游戏
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class SortDlg extends Dialog {
      Label m_labelSort[],m_labelName[],m_labelScore[];
      public SortDlg(Frame f,int nScore[],String strName[]){
         super(f,"英雄榜",true);
         m_labelSort=new Label[4];
         m_labelName=new Label[4];
         m_labelScore=new Label[4];
         setLayout(new GridLayout(4,3));
         m_labelSort[0]=new Label("名次");add(m_labelSort[0]);
         m_labelName[0]=new Label("姓名");add(m_labelName[0]); 
         m_labelScore[0]=new Label("名次");add(m_labelScore[0]);
         for(int i=0;i<3;i++){
             m_labelSort[i+1]=new Label("第"+(i+1)+"名");add(m_labelSort[i+1]);
             m_labelName[i+1]=new Label(strName[i]);add(m_labelName[i+1]); 
             m_labelScore[i+1]=new Label(""+nScore[i]);add(m_labelScore[i+1]);
         }
         setSize(200,160);
         setResizable(false);
         addWindowListener(new WindowAdapter(){
                                public void windowClosing(WindowEvent e){
                                     setVisible(false);
                                     dispose();
                                }
                           });
         setVisible(true);
     }
}

class NameDlg extends Dialog implements ActionListener,TextListener{
     String m_strName;
     TextField m_textName;
     Button m_bnOk,m_bnCancel;
     boolean m_bIsOk;
     public NameDlg(Frame f){
         super(f,"输入你的姓名",true);
         Panel p1=new Panel(),p2=new Panel();
         setLayout(new GridLayout(2,1));
         p1.setLayout(new GridLayout(1,3));
         p2.setLayout(new GridLayout(1,5));
         Label name=new Label("姓  名:",Label.RIGHT);
         m_textName=new TextField(20);
         m_bnOk=new Button("确定");
         m_bnCancel=new Button("取消");
         p1.add(name);p1.add(m_textName);p1.add(new Label());
         p2.add(new Label());p2.add(m_bnOk);p2.add(new Label());p2.add(m_bnCancel);
         p2.add(new Label());
         add(p1);add(p2);
         m_textName.addTextListener(this);
         m_bnOk.addActionListener(this);m_bnOk.setEnabled(false);
         m_bnCancel.addActionListener(this);
         setSize(200,80);
         setResizable(false);
         addWindowListener(new WindowAdapter(){
                                public void windowClosing(WindowEvent e){
                                     setVisible(false);
                                     dispose();
                                }
                           });
         setVisible(true);
     }
     public void textValueChanged(TextEvent e){
         if(e.getSource()==m_textName){
            
            if((m_textName.getText().length())>0)m_bnOk.setEnabled(true);
            else m_bnOk.setEnabled(false);
         }
     }
     public void actionPerformed(ActionEvent e){
         if(e.getSource()==m_bnOk){
           m_bIsOk=true;
           m_strName=m_textName.getText();
           setVisible(false);
         }
         else if(e.getSource()==m_bnCancel){
           m_bIsOk=false;
           m_strName=null;
           setVisible(false);
         }
    }
}
         
public class FileOperator{
   protected int m_anScore[];
   protected RandomAccessFile m_fileAccess;
   protected Frame m_fParent;
   protected String m_strName[];
   public FileOperator (String str,Frame f){
      m_fParent =f;
      m_anScore=new int[3];
      m_strName=new String[3];
      File file=new File(str);
      if(file.exists()){
         try{m_fileAccess=new RandomAccessFile(file,"rw");}
         catch(FileNotFoundException ie){};
         try{m_fileAccess.seek(0);}
         catch(IOException ie){};
         for(int i=0;i<3;i++){
            try{m_anScore[i]=m_fileAccess.readInt();
                m_strName[i]=m_fileAccess.readUTF();
            }
            catch(IOException ie){};
         }
      }
      else {
         try{m_fileAccess=new RandomAccessFile(file,"rw");} 
         catch(FileNotFoundException ie){};
         
         for(int i=0;i<3;i++){
             try{m_fileAccess.writeInt(0);}
             catch(IOException ie){};
             m_anScore[i]=0;
             try{m_fileAccess.writeUTF("***");}
             catch(IOException ie){};
             m_strName[i]="***";
         }
      }
   }
   public boolean compare(int nNewScore){
       return nNewScore>m_anScore[2];
   }
   public void insert(int nNewScore){
      int nScore;
      String strName=null;
      NameDlg enterName=new NameDlg(m_fParent);
      if(enterName.m_bIsOk==true){
          m_anScore[2]=nNewScore;
          m_strName[2]=enterName.m_strName;
          for(int i=2;i>=1;i--){
              if(m_anScore[i]>m_anScore[i-1]){
                  nScore=m_anScore[i];m_anScore[i]=m_anScore[i-1];m_anScore[i-1]=nScore;
                  strName=m_strName[i];m_strName[i]=m_strName[i-1];m_strName[i-1]=strName;
              }
          }
          try{m_fileAccess.seek(0);}
          catch(IOException ie){};
          for(int j=0;j<3;j++){
            try{m_fileAccess.writeInt(m_anScore[j]);
                m_fileAccess.writeUTF(m_strName[j]);
            }
            catch(IOException ie){};
         }
      }
      enterName.dispose();
    }
    public void showScore(){
       SortDlg sort=new SortDlg(m_fParent,m_anScore,m_strName);
       
    }
    public void close(){
      try{m_fileAccess.close();}
      catch(IOException ie){};
    }
}

        

⌨️ 快捷键说明

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