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

📄 hero.java

📁 希望大家多多交流
💻 JAVA
字号:
package system;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.io.*;

//排行榜类
public class Hero
    extends JDialog {
  private static JFrame owner;
  public static final int N = 10; //排行榜保存前十名的信息

  private static final int WIDTH = 300;
  private static final int HEIGHT = 230;

  public Hero(JFrame owner) {
    super(owner, true);
    this.owner = owner;

    this.initUI();
  }

  //初始化界面
  public void initUI(){
    String[] title = {"玩家名称","得分"};
    String[][] data = this.getHeroInfos();
    UneditableModel m = new UneditableModel(data,title);
    JTable t = new JTable(m);
    t.setRowSelectionAllowed(false);
    t.setColumnSelectionAllowed(false);

    int v = JScrollPane.VERTICAL_SCROLLBAR_NEVER;
    int h = JScrollPane.HORIZONTAL_SCROLLBAR_NEVER;
    JScrollPane p = new JScrollPane(t,v,h);

    this.getContentPane().add(p);
  }

  //读取排行榜信息
  public static String[][] getHeroInfos() {
    Properties p = new Properties();

    try {
      p.load(new FileInputStream("h.inf"));
    }
    catch (IOException e) {
      e.printStackTrace();
    }

    String[][] data = new String[10][2];
    //读取完家名称
    for (int i = 1; i <= Hero.N; i++) {
      data[i - 1][0] = p.getProperty(i + "_name");
    }
    //读取完家得分
    for (int i = 1; i <= Hero.N; i++) {
      data[i - 1][1] = p.getProperty(i + "_score");
    }

    return data;
  }

  //更新排行榜信息
  public static void updateHeroInfos(String name, int score) {
    String[][] data = Hero.getHeroInfos();
    Vector v_name = new Vector();
    Vector v_score = new Vector();

    for(int i = 0; i < Hero.N; i++){
      v_name.add(data[i][0]);//名称
      v_score.add(data[i][1]);//得分
    }

    int min = Integer.parseInt( (String) v_score.get(Hero.N - 1));
    if (score < min) {
      JOptionPane.showMessageDialog(Hero.owner, "很遗憾,你没能进入排行榜。继续加油吧!",
                                    "提示", JOptionPane.INFORMATION_MESSAGE);
      return;
    }

    for (int i = 0; i < v_score.size(); i++) {
      int s = Integer.parseInt( (String) v_score.get(i));
      if (score >= s) {
        v_name.add(i,name);
        v_score.add(i,score + "");
        JOptionPane.showMessageDialog(Hero.owner, "恭喜你,你得了第"+i+1+"名!",
                                    "恭喜!", JOptionPane.INFORMATION_MESSAGE);
        break;
      }
    }

    Properties p = new Properties();

    try {
      p.load(new FileInputStream("h.inf"));
    }
    catch (IOException e) {
      e.printStackTrace();
    }

    //保存排行榜前十名的信息
    for (int i = 1; i <= Hero.N; i++) {
      p.setProperty(i+"_name",(String)v_name.get(i - 1));
      p.setProperty(i + "_score", (String) v_score.get(i - 1));
    }

    try{
      p.store(new FileOutputStream("h.inf"),"Hero");
    }
    catch(IOException e){
      e.printStackTrace();
    }
  }

  public static void showHeroDialog(JFrame owner){
    new Hero(owner).show();
  }

  public void show() {
    this.setTitle("高分排行榜");
    this.setSize(this.WIDTH, this.HEIGHT);
    this.setResizable(false);

    Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
    int width = s.width;
    int height = s.height;
    this.setLocation( (width - this.WIDTH) / 2, (height - this.HEIGHT) / 2);

    super.show();
  }

  //一个使表的单元格不可编辑的表模型
  private class UneditableModel
      extends DefaultTableModel {
    public UneditableModel(String[][] data,String[] title){
      super(data,title);
    }

    public boolean isCellEditable(int row, int col) {
      return false;
    }
  }
}

⌨️ 快捷键说明

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