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

📄 scoresystemmain.java

📁 Java编程技巧
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2003
 * Company:
 * @author
 * @version 1.0
 */

////应用程序类
public class ScoreSystemMain extends JFrame implements ActionListener
{
  MatchSeason matchseason;///当前赛季
  String fullname;///当前赛季所存储的的文件(包括路径名)
  int currentsports;///当前赛季的当前运动会索引

  //////////以下所有东西用于显示当前运动会的所有个人选项和花样选项表及赛程表
  String headplayer[]={"代号","姓名","年龄","籍贯","电话号码","个人简述"};
  String headstyle[]={"花样名称","难度系数","扼要描述"};
  String headplay[]={"比赛项目名称","花样名称","比赛地点","比赛开始时间","所参加选手","所参加裁判","各裁判对各选手的评分"};
  ///这是表的标题
  Object playerdata[][],styledata[][],playdata[][];
  ///这是所需数据
  JTable tplayer,tstyle,tplay;
  JScrollPane jsplayer,jsstyle,jsplay;
  JPanel main;
  ///这是所用的控件
  //////////以上所有东西用于显示当前运动会的所有个人选项和花样选项表及赛程表

  Container pane;////保存主界面的容器
  String sportsdate,sportsplace,sportsname;/// 保存当前运动会的日期、地点及名称
  JTextField tfdate,tfplace,tfsports;/// 用于显示当前运动会的日期、地点及名称
  boolean revised=false;////数据是否被修改

  //////判定字符串s是否位于字符串数组ss中:不在,返回-1;在,返回在数组中的索引下标
  public int isInSet(String s,String ss[])
  {
    if(ss==null||s==null) return -1;
    for(int i=0;i<ss.length;i++)
      if(ss[i].equalsIgnoreCase(s)) return i;
    return -1;
  }

  ////从当前运动会得到要显示的数据,分别存储在sportsdate,sportsplace,sportsname,playerdata,styledata,playdata中
  private void getData(MatchSeason season,int i)
  {
    if(season==null||season.sports==null||season.sports.length<=i)
    {
      sportsdate="";
      sportsplace="";
      sportsname="";
      playerdata=null;
      styledata=null;
      playdata=null;
      return;
    }

    Sports s=season.sports[i];
    if(s==null) return;

    int j,k,r;
    sportsdate=s.sportsdate;
    sportsplace=s.sportsplace;
    sportsname=s.name;
    if(s.play!=null)
    {
      playdata=new Object[s.play.length][];
      for(j=0;j<s.play.length;j++)
      {
        playdata[j]=new Object[7];
        playdata[j][0]=s.play[j].program;
        playdata[j][1]=s.play[j].style;
        playdata[j][2]=s.play[j].place;
        playdata[j][3]=s.play[j].date;

        String str="";
        int len;
        if(s.play[j].player!=null)
        {
          len=s.play[j].player.length-1;
          for(r=0;r<len;r++)
          {
            str+=s.play[j].player[r];
            str+=",";
          }
          str+=s.play[j].player[len];
        }
        playdata[j][4]=str;

        str="";
        if(s.play[j].referee!=null)
        {
          len=s.play[j].referee.length-1;
          for(r=0;r<len;r++)
          {
            str+=s.play[j].referee[r];
            str+=",";
          }
          str+=s.play[j].referee[len];
        }
        playdata[j][5]=str;

        str="";
        if(s.play[j].netscore!=null)
        {
          for(k=0;k<s.play[j].netscore.length;k++)
          {
            if(s.play[j].netscore[k]==null) continue;
            len=s.play[j].netscore[k].length-1;
            for(r=0;r<len;r++)
            {
              str+=new Double(s.play[j].netscore[k][r]).toString();
              str+=",";
            }
            str+=new Double(s.play[j].netscore[k][len]).toString();
            str+=";";
          }
        }
        playdata[j][6]=str;
      }
    }
    else playdata=null;

    if(s.style!=null)
    {
      styledata=new Object[s.style.length][];
      for(j=0;j<s.style.length;j++)
      {
        styledata[j]=new Object[3];
        styledata[j][0]=s.style[j].name;
        styledata[j][1]=new Double(s.style[j].hardfactor);
        styledata[j][2]=s.style[j].depiction;
      }
    }
    else styledata=null;

    if(s.player!=null)
    {
      playerdata=new Object[s.player.length][];
      for(j=0;j<s.player.length;j++)
      {
        playerdata[j]=new Object[6];
        playerdata[j][0]=s.player[j].no;
        playerdata[j][1]=s.player[j].name;
        playerdata[j][2]=new Integer(s.player[j].age);
        playerdata[j][3]=s.player[j].address;
        playerdata[j][4]=s.player[j].phonenumber;
        playerdata[j][5]=s.player[j].depiction;
      }
    }
    else playerdata=null;
  }

  ////显示表格,标题在head中,表格数据在data中
  public JTable showTable(String []head,Object [][]data,JScrollPane sp,int i)
  {
    if(data==null) return null;
    JTable t=new JTable(data,head);
    sp = new JScrollPane(t);
    main.add(sp,i);
    return t;
  }

  ////显示数据,showData(0)表示小范围刷新数据;showData(1)表示大范围刷新数据
  public void showData()
  {
    showData(0);
  }

  public void showData(int k)
  {
    getData(matchseason,currentsports);////从当前运动会获得数据

    int i=0;
    main.removeAll();
    if(playerdata!=null) tplayer=showTable(headplayer,playerdata,jsplayer,i++);
    else tplayer=null;
    if(styledata!=null) tstyle=showTable(headstyle,styledata,jsstyle,i++);
    else tstyle=null;
    if(playdata!=null) tplay=showTable(headplay,playdata,jsplay,i++);
    else tplay=null;

    tfdate.setText(sportsdate);
    tfplace.setText(sportsplace);
    tfsports.setText(sportsname);

    if(k==1)
    {
      main.setVisible(false);
      main.setVisible(true);
    }
    else this.show();
  }

  ////检查数据的完整性
  public void checkData()
  {
  }

  ////退出函数
  private void quit()
  {
    if(revised)
    {
      int ret=JOptionPane.showConfirmDialog(this,"数据已被修改,是否保存修改?");
      if(ret==JOptionPane.YES_OPTION)
        saveFile(0);
      else if(ret==JOptionPane.CANCEL_OPTION)
        return;
    }
    System.exit(0);
  }

  ///判断文件是否已经存在?
  public boolean isFileExist(String name,String dir)
  {
    return new File(dir,name).exists();
  }

  ///从所有控件得到数据并将其保存下来
  public void saveAll()
  {
    if(matchseason==null||matchseason.sports==null) return;

    saveData();

    int temp=currentsports;
    for(int i=0;i<matchseason.sports.length;i++)
    {
      if(i!=temp)
      {
        currentsports=i;
        showData();
        saveData();
      }
    }
    currentsports=temp;
    showData();
  }

  ////保存当前运动会数据
  public void saveData()
  {
    if(matchseason==null||matchseason.sports==null||matchseason.sports.length<=currentsports) return;

    checkData();

    Sports s=new Sports(tfsports.getText(),tfdate.getText(),tfplace.getText());
    int i,j;

    if(tplayer!=null&&tplayer.getRowCount()>0)
    {
      s.player=new Player[tplayer.getRowCount()];
      for(i=0;i<tplayer.getRowCount();i++)
      {
        s.player[i]=new Player(tplayer.getValueAt(i,0).toString());
        s.player[i].name=tplayer.getValueAt(i,1).toString();
        s.player[i].age=Integer.parseInt(tplayer.getValueAt(i,2).toString());
        s.player[i].address=tplayer.getValueAt(i,3).toString();
        s.player[i].phonenumber=tplayer.getValueAt(i,4).toString();
        s.player[i].depiction=tplayer.getValueAt(i,5).toString();
      }
    }

    if(tplay!=null&&tplay.getRowCount()>0)
    {
      s.play=new Play[tplay.getRowCount()];
      for(i=0;i<tplay.getRowCount();i++)
      {
        String str;
        s.play[i]=new Play(tplay.getValueAt(i,0).toString(),tplay.getValueAt(i,1).toString(),tplay.getValueAt(i,2).toString(),tplay.getValueAt(i,3).toString());

        str=tplay.getValueAt(i,4).toString();
        if(!str.equals("")) s.play[i].player=split(str);
        str=tplay.getValueAt(i,5).toString();
        if(!str.equals("")) s.play[i].referee=split(str);
        str=tplay.getValueAt(i,6).toString();
        if(!str.equals(""))
        {
          String []ss=split(str,";");
          String []net;
          if(ss!=null)
          {
            s.play[i].netscore=new double[ss.length][];
            for(j=0;j<ss.length;j++)
            {
              net=split(ss[j]);
              if(net!=null)
              {
                s.play[i].netscore[j]=new double[net.length];
                for(int k=0;k<net.length;k++)
                  s.play[i].netscore[j][k]=Double.parseDouble(net[k]);
              }
            }
          }
        }
      }
    }

    if(tstyle!=null&&tstyle.getRowCount()>0)
    {
      s.style=new ProgramStyle[tstyle.getRowCount()];
      for(i=0;i<tstyle.getRowCount();i++)
      {
        s.style[i]=new ProgramStyle(tstyle.getValueAt(i,0).toString());
        s.style[i].hardfactor=Double.parseDouble(tstyle.getValueAt(i,1).toString());
        s.style[i].depiction=tstyle.getValueAt(i,2).toString();
      }
    }

    matchseason.sports[currentsports]=s;
  }

  ///////返回字符串str用dim分割开后的字符串数组
  public String[] split(String str,String dim)
  {
    StringTokenizer st=new StringTokenizer(str,dim);
    String s[]=new String[st.countTokens()];
    int i=0;
    while(st.hasMoreTokens())
    {
      s[i]=st.nextToken();
      i++;
    }
    return s;
  }
  public String[] split(String str)
  {
    return split(str,",");
  }

  ////////新建文件时调用
  private void newFile()
  {
    FileDialog fdlg=new FileDialog(this,"新建赛季数据文件...",FileDialog.LOAD);
    fdlg.setVisible(true);

    String filename=fdlg.getFile();
    if(filename==null) return;
    String dir=fdlg.getDirectory();
    fullname=dir+filename;

    if(isFileExist(filename,dir))
    {
      int ret=JOptionPane.showConfirmDialog(this,"是否覆盖原文件?");
      if(ret!=JOptionPane.YES_OPTION) return;
    }

    matchseason=new MatchSeason();
    revised=true;
    this.setTitle(fullname);
  }

  ///保存文件时调用,i=1表示另存为;i=0时就表示保存
  private void saveFile(int i)
  {
    String temp=fullname;
    if(i==1||fullname==null)
    {
      FileDialog fdlg=new FileDialog(this,"保存当前赛季数据...",FileDialog.SAVE);
      fdlg.setVisible(true);

      String filename=fdlg.getFile();
      if(filename==null) return;
      String dir=fdlg.getDirectory();
      fullname=dir+filename;


      if(i==0&&isFileExist(filename,dir))
      {
        int ret=JOptionPane.showConfirmDialog(this,"是否覆盖原文件?");
        if(ret!=JOptionPane.YES_OPTION) return;
      }
    }

    saveAll();
    try
    {
      //setup the streams
      File outFile = new File(fullname);
      FileOutputStream outFileStream = new FileOutputStream(outFile);
      ObjectOutputStream outObjectStream  = new ObjectOutputStream(outFileStream);
      outObjectStream.writeObject(matchseason);
      outObjectStream.close();
    }
    catch (IOException pe)
    {
      JOptionPane.showMessageDialog(this,"保存失败!!!!");
      System.out.println(pe);
      fullname=temp;
      return;
    }

    if(i==1) this.setTitle(fullname);
    revised=false;
  }

  ////打开文件时调用
  private void openFile()
  {
      FileDialog fdlg=new FileDialog(this,"打开赛季数据文件...",FileDialog.LOAD);
      fdlg.setVisible(true);

      String temp=fullname;
      String filename=fdlg.getFile();
      if(filename==null) return;
      String dir=fdlg.getDirectory();
      fullname=dir+filename;

      MatchSeason tempseason=matchseason;
      try
      {
          //setup file and stream
          File inFile = new File(fullname);
          FileInputStream inFileStream = new FileInputStream(inFile);
          ObjectInputStream inObjectStream = new ObjectInputStream(inFileStream);
          matchseason = (MatchSeason) inObjectStream.readObject();
          inObjectStream.close();
      }
      catch(IOException pe)
      {
        System.out.println("打开文件失败!!!!");
        System.out.println(pe.getMessage());
        matchseason=tempseason;
        fullname=temp;

⌨️ 快捷键说明

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