快捷搜索: 长连接 前端 源码 pan

javaswing实现扫雷游戏

需要的小伙伴可以找我帮助您运行和调试,v:duole1314

MineGame.java(程序入口)

package cn.itbaizhan.mine;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;

public class MineGame extends JFrame implements ActionListener {
	JMenuBar bar;
	JMenu fileMenu1, fileMenu2;
	JMenuItem 初级, 中级, 高级, 自定义, 扫雷榜;
	JMenuItem 介绍, 玩法;
	MineArea mineArea = null;
	File 英雄榜 = new File("英雄榜.txt");
	Hashtable hashtable = null;
	ShowRecord showHeroRecord = null;
	JDialog set = null;
	JPanel panel, panel1, panel2, panel3, panel4;
	JLabel label, label1, label2, label3;
	JTextField row = null, column = null, mine = null;
	JButton 确认,取消;
	JDialog introduce = null, play = null;
	JLabel label4, label5;
	
	MineGame() {
		mineArea = new MineArea(16, 16, 40, 2);
		add(mineArea, BorderLayout.CENTER); // 边框布局
		bar = new JMenuBar();
		fileMenu1 = new JMenu("游戏");
		初级 = new JMenuItem("初级");
		中级 = new JMenuItem("中级");
		高级 = new JMenuItem("高级");
		自定义 = new JMenuItem("自定义");
		扫雷榜 = new JMenuItem("扫雷榜");
		fileMenu1.add(初级);
		fileMenu1.add(中级);
		fileMenu1.add(高级);
		fileMenu1.add(自定义);
		fileMenu1.add(扫雷榜);
		fileMenu2 = new JMenu("帮助");
		介绍 = new JMenuItem("介绍");
		玩法 = new JMenuItem("玩法");
		fileMenu2.add(介绍);
		fileMenu2.add(玩法);
		bar.add(fileMenu1);
		bar.add(fileMenu2);
		setJMenuBar(bar); // 设置窗体的菜单栏
		初级.addActionListener(this);
		中级.addActionListener(this);
		高级.addActionListener(this);
		自定义.addActionListener(this);
		扫雷榜.addActionListener(this);
		介绍.addActionListener(this);
		玩法.addActionListener(this);
		hashtable = new Hashtable();
		hashtable.put("初级", "初级#" + 999 + "#匿名");
		hashtable.put("中级", "中级#" + 999 + "#匿名");
		hashtable.put("高级", "高级#" + 999 + "#匿名");
		if (!英雄榜.exists()) {
			try {
				FileOutputStream out = new FileOutputStream(英雄榜);
				ObjectOutputStream objectOut = new ObjectOutputStream(out);
				objectOut.writeObject(hashtable);
				objectOut.close();
				out.close();
			} catch (IOException e) {
			}
		}
		showHeroRecord = new ShowRecord(this, hashtable);
		setBounds(300, 100, 480, 560); // 移动组件并调整大小
		setVisible(true); // 使Window可见
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭Window的同时关闭资源
		validate(); // 再次布置子组件
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == 初级) {
			mineArea.initMineArea(9, 9, 10, 1);
			setBounds(300, 100, 270, 350);
		}
		if (e.getSource() == 中级) {
			mineArea.initMineArea(16, 16, 40, 2);
			setBounds(300, 100, 480, 560);
		}
		if (e.getSource() == 高级) {
			mineArea.initMineArea(16, 30, 99, 3);
			setBounds(100, 100, 900, 560);
		}
		if (e.getSource() == 自定义) {
			set = new JDialog();
			set.setTitle("自定义难度");
			set.setBounds(300,100,300,130);
			set.setResizable(false);//设置大小不可变
			set.setModal(true);//设置为对话框模式
			panel = new JPanel();
			//panel.setLayout(new BorderLayout());
			panel1 = new JPanel();
			panel2 = new JPanel();
			panel3 = new JPanel();
			panel4 = new JPanel();
			label = new JLabel("请输入行列数与地雷数:", JLabel.CENTER); 
			label1 = new JLabel("行:", JLabel.CENTER); 
			label2 = new JLabel("列:", JLabel.CENTER); 
			label3 = new JLabel("地雷数:", JLabel.CENTER); 
			row = new JTextField();
			row.setText("16");
			row.setSize(1, 6);
			column = new JTextField();
			column.setText("16");
			mine = new JTextField();
			mine.setText("40");
			确认 = new JButton("确认");
			确认.addActionListener(this);
			取消 = new JButton("取消");
			取消.addActionListener(this);
			panel1.add(label1);
			panel1.add(row);
			panel2.add(label2);
			panel2.add(column);
			panel3.add(label3);
			panel3.add(mine);
			panel4.add(确认);
			panel4.add(取消);
			panel.add(panel1);
			panel.add(panel2);
			panel.add(panel3);
			set.add(label, BorderLayout.NORTH);
			set.add(panel, BorderLayout.CENTER);
			set.add(panel4, BorderLayout.SOUTH);
			set.setVisible(true);
		}
		if (e.getSource() == 扫雷榜) {
			if (showHeroRecord != null)
				showHeroRecord.setVisible(true);
		}
		if (e.getSource() == 确认) {
			int rowNum = Integer.parseInt(row.getText());
			int columnNum = Integer.parseInt(column.getText());
			int mineNum = Integer.parseInt(mine.getText());
			if(rowNum < 9)
				rowNum = 9;
			if(rowNum > 16)
				rowNum = 16;
			if(columnNum < 9)
				columnNum = 9;
			if(columnNum > 30)
				columnNum = 30;
			if(mineNum < 10)
				mineNum = 10;
			if(mineNum > 99)
				mineNum = 99;
			mineArea.initMineArea(rowNum, 
					columnNum, mineNum, 4);
			setBounds(100, 100, columnNum * 30, rowNum * 30 + 80);
			set.setVisible(false);
		}
		if (e.getSource() == 取消) {
			set.setVisible(false);
		}
		if (e.getSource() == 介绍) {
			introduce = new JDialog();
			introduce.setTitle("扫雷介绍");
			introduce.setBounds(300,100,300,300);
			introduce.setResizable(false);
			introduce.setModal(true);
			label4 = new JLabel();
			label4.setSize(280, 250);
			label4.setText("<html><body>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp扫雷最原始的版本可以追溯到1973年"
					+ "一款名为“方块”的游戏。不久,“方块”被改写成了游戏“Rlogic”。在“Rlogic”里,玩家的任务是作为美国"
					+ "海军陆战队队员,为指挥中心探出一条没有地雷的安全路线,如果路全被地雷堵死就算输。"
					+ "两年后,汤姆·安德森在“Rlogic”的基础上又编写出了游戏“地雷”,由此奠定了现代扫雷游戏的雏形。"
					+ "1981年,微软公司的罗伯特·杜尔和卡特·约翰逊两位工程师在Windows3.1系统上加载了该游戏,"
					+ "扫雷游戏才正式在全世界推广开来。</body></html>");
			introduce.add(label4);
			introduce.setVisible(true);
		}
		if (e.getSource() == 玩法) {
			play = new JDialog();
			play.setTitle("游戏玩法");
			play.setBounds(300,100,300,300);
			play.setResizable(false);
			play.setModal(true);
			label4 = new JLabel();
			label4.setSize(280, 250);
			label4.setText("<html><body>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp扫游戏目标是在最短的时间内"
					+ "根据点击格子出现的数字找出所有非雷格子,同时避免踩雷。<br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"
					+ "&nbsp&nbsp当玩家点开一个格子时,会在该格子上显示周围8个格子的雷数"
					+ "(如果没有雷则自动点开周围的格子),玩家需要通过这些数字来判断雷的位置,"
					+ "将是雷的格子标记为小红旗。当所有地雷被标记且非雷格子都被点开时游戏胜利。</body></html>");
			play.add(label4);
			play.setVisible(true);
		}
		validate();
	}

	public static void main(String args[]) {
		new MineGame();
	}
}

MineArea.java

package cn.itbaizhan.mine;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MineArea extends JPanel implements ActionListener, MouseListener {
	JButton reStart;
	Block[][] block;
	BlockView[][] blockView;
	LayMines lay;
	int row, colum, mineCount, markMount;// 雷区的行数、列数以及地雷个数和用户给出的标记数
	ImageIcon mark;
	int grade; // 初、中、高级
	JPanel pCenter, pNorth;
	JTextField showTime, showMarkedMineCount; // 显示用时以及标记数
	Timer time; // 计时器
	int spendTime = 0;
	Record record;
	JDialog lose = new JDialog();
	JPanel panel;
	JLabel str;	
	JButton restart1 = new JButton("重新开始");
	JButton	cancel = new JButton("取消");

	public MineArea(int row, int colum, int mineCount, int grade) {
		reStart = new JButton("重新开始");
		mark = new ImageIcon("mark.png"); // 探雷标记
		time = new Timer(1000, this);
		showTime = new JTextField(5);
		showMarkedMineCount = new JTextField(5);
		showTime.setHorizontalAlignment(JTextField.CENTER);
		showTime.setFont(new Font("Arial", Font.BOLD, 16));
		showTime.setDisabledTextColor(java.awt.Color.black); // 设置字体颜色
		showTime.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray));// 设置边框颜色
		showTime.setEnabled(false);// 设置不能响应用户输入
		showMarkedMineCount.setHorizontalAlignment(JTextField.CENTER);
		showMarkedMineCount.setFont(new Font("Arial", Font.BOLD, 16));
		showMarkedMineCount.setDisabledTextColor(java.awt.Color.black);
		showMarkedMineCount.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray));
		showMarkedMineCount.setEnabled(false);
		pCenter = new JPanel();
		pNorth = new JPanel();
		lay = new LayMines();
		initMineArea(row, colum, mineCount, grade); // 初始化雷区,见下面的LayMines()
		reStart.addActionListener(this);
		pNorth.add(showMarkedMineCount);
		pNorth.add(reStart);
		pNorth.add(showTime);
		setLayout(new BorderLayout());
		add(pNorth, BorderLayout.NORTH);
		add(pCenter, BorderLayout.CENTER);
	}

	public void initMineArea(int row, int colum, int mineCount, int grade) {
		pCenter.removeAll();
		spendTime = 0;
		markMount = mineCount;
		this.row = row;
		this.colum = colum;
		this.mineCount = mineCount;
		this.grade = grade;
		block = new Block[row][colum];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < colum; j++)
				block[i][j] = new Block();
		}
		//lay.layMinesForBlock(block, mineCount);
		blockView = new BlockView[row][colum];
		pCenter.setLayout(new GridLayout(row, colum));
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < colum; j++) {
				blockView[i][j] = new BlockView();
				//blockView[i][j].giveView(block[i][j]); // 给block[i][j]提供视图
				pCenter.add(blockView[i][j]);
				blockView[i][j].getBlockCover().addActionListener(this);
				blockView[i][j].getBlockCover().addMouseListener(this);
				blockView[i][j].seeBlockCover();
				blockView[i][j].getBlockCover().setEnabled(true);
				blockView[i][j].getBlockCover().setIcon(null);
			}
		}
		showMarkedMineCount.setText("" + markMount);
		validate();
	}

	public void initMine(int m, int n){
		lay.layMinesForBlock(block, mineCount, m, n);
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < colum; j++) {
				blockView[i][j].giveView(block[i][j]);
			}
		}
	}
	
	public void setRow(int row) {
		this.row = row;
	}

	public void setColum(int colum) {
		this.colum = colum;
	}

	public void setMineCount(int mineCount) {
		this.mineCount = mineCount;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == restart1) {
			initMineArea(row, colum, mineCount, grade);
			lose.dispose();
			return;
		}
		if (e.getSource() == cancel){
			lose.dispose();
			return;
		}
		int temp = 0;
		if (e.getSource() != reStart && e.getSource() != time) {
			time.start();
			int m = -1, n = -1;
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < colum; j++) {
					if(block[i][j].getIsMine() == true)
						temp = -1;
					if (e.getSource() == blockView[i][j].getBlockCover()) {
						m = i;
						n = j;
					}
				}
			}
			
			if(temp == 0)
				initMine(m, n);
			
			if (block[m][n].getIsMine()) {
				for (int i = 0; i < row; i++) {
					for (int j = 0; j < colum; j++) {
						blockView[i][j].getBlockCover().setEnabled(false);
						if (block[i][j].getIsMine())
							blockView[i][j].seeBlockNameOrIcon();
					}
				}
				panel = new JPanel();
				panel.setLayout(new FlowLayout());
				str = new JLabel("你输了哦!", JLabel.CENTER);
				//restart1 = new JButton("重新开始");
				restart1.addActionListener(this);
				//cancel = new JButton("取消");
				cancel.addActionListener(this);
				lose.setTitle("输了");
				lose.setBounds(300,100,200,150);
				lose.setResizable(false);
				lose.setVisible(false);
				lose.setModal(true);
				time.stop();
				spendTime = 0;
				markMount = mineCount;
				lose.add(str, BorderLayout.CENTER);
				panel.add(restart1);
				panel.add(cancel);
				lose.add(panel, BorderLayout.SOUTH);
				lose.setVisible(true);
			} else {
				show(m, n); // 见本类后面的show方法
			}
		}
		if (e.getSource() == reStart) {
			initMineArea(row, colum, mineCount, grade);
		}
		if (e.getSource() == time) {
			spendTime++;
			showTime.setText("" + spendTime);
		}
		inquireWin();
	}

	public void show(int m, int n) {
		if (block[m][n].getAroundMineNumber() > 0 && block[m][n].getIsOpen() == false) {
			blockView[m][n].seeBlockNameOrIcon();
			block[m][n].setIsOpen(true);
			return;
		} else if (block[m][n].getAroundMineNumber() == 0 && block[m][n].getIsOpen() == false) {
			blockView[m][n].seeBlockNameOrIcon();
			block[m][n].setIsOpen(true);
			for (int k = Math.max(m - 1, 0); k <= Math.min(m + 1, row - 1); k++) {
				for (int t = Math.max(n - 1, 0); t <= Math.min(n + 1, colum - 1); t++)
					show(k, t);
			}
		}
	}

	public void mousePressed(MouseEvent e) {
		JButton source = (JButton) e.getSource();
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < colum; j++) {
				if (e.getModifiers() == InputEvent.BUTTON3_MASK && source == blockView[i][j].getBlockCover()) {
					if (block[i][j].getIsMark()) {
						source.setIcon(null);
						block[i][j].setIsMark(false);
						markMount = markMount + 1;
						showMarkedMineCount.setText("" + markMount);
					} else {
						source.setIcon(mark);
						block[i][j].setIsMark(true);
						markMount = markMount - 1;
						showMarkedMineCount.setText("" + markMount);
					}
				}
			}
		}
	}

	public void inquireWin() {
		int number = 0;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < colum; j++) {
				if (block[i][j].getIsOpen() == false)
					number++;
			}
		}
		if (number == mineCount) {
			time.stop();
			record = new Record();
			switch (grade) {
			case 1:
				record.setGrade("初级");
				break;
			case 2:
				record.setGrade("中级");
				break;
			case 3:
				record.setGrade("高级");
				break;
			}
			record.setTime(spendTime);
			record.setVisible(true);
		}

	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mouseClicked(MouseEvent e) {
	}
}

LayMines.java

package cn.itbaizhan.mine;

import java.util.*;
import javax.swing.*;

public class LayMines {
	ImageIcon mineIcon;

	LayMines() {
		mineIcon = new ImageIcon("mine.gif");
	}

	public void layMinesForBlock(Block block[][], int mineCount, int m, int n) {
		int row = block.length;
		int column = block[0].length;
		LinkedList<Block> list = new LinkedList<Block>();
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++)
				list.add(block[i][j]);
		}
		while (mineCount > 0) {
			int size = list.size(); // list返回节点的个数
			int randomIndex = (int) (Math.random() * size);
			Block b = list.get(randomIndex);
			if(b != block[m][n]){
				b.setIsMine(true); // 设雷
				b.setMineIcon(mineIcon);
				list.remove(randomIndex); // list删除索引值为randomIndex的节点
				mineCount--;
			}
		}
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++) {
				if (block[i][j].getIsMine()) {
					block[i][j].setIsOpen(false);
					block[i][j].setIsMark(false);
				} else {
					// 不是雷的格计算雷的数目
					int mineNumber = 0;
					for (int k = Math.max(i - 1, 0); k <= Math.min(i + 1, row - 1); k++) {
						for (int t = Math.max(j - 1, 0); t <= Math.min(j + 1, column - 1); t++) {
							if (block[k][t].getIsMine())
								mineNumber++;
						}
					}
					block[i][j].setIsOpen(false);
					block[i][j].setIsMark(false);
					block[i][j].setName("" + mineNumber);
					block[i][j].setAroundMineNumber(mineNumber);
				}
			}
		}
	}
}

BlockView.java

package cn.itbaizhan.mine;

import javax.swing.*;
import java.awt.*;

public class BlockView extends JPanel {
	JLabel blockNameOrIcon; // 用来显示Block对象的name、number和mineIcon属性
	JButton blockCover; // 用来遮挡blockNameOrIcon.
	CardLayout card; // 卡片式布局,显示第一次添加的组件
	ImageIcon icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8;
	
	BlockView() {
		card = new CardLayout();
		setLayout(card);
		blockNameOrIcon = new JLabel("", JLabel.CENTER);
		blockNameOrIcon.setHorizontalTextPosition(AbstractButton.CENTER);
		blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER);
		blockCover = new JButton();
		add("cover", blockCover);
		add("view", blockNameOrIcon);
	}

	public void giveView(Block block) {
		if (block.isMine) {
			blockNameOrIcon.setIcon(block.getMineicon());
		} else {
			int n = block.getAroundMineNumber();
			if (n == 1) {
				icon1 = new ImageIcon("1.jpg");
				blockNameOrIcon.setIcon(icon1);
			}
			if (n == 2) {
				icon2 = new ImageIcon("2.jpg");
				blockNameOrIcon.setIcon(icon2);
			}
			if (n == 3) {
				icon3 = new ImageIcon("3.jpg");
				blockNameOrIcon.setIcon(icon3);
			}
			if (n == 4) {
				icon4 = new ImageIcon("4.jpg");
				blockNameOrIcon.setIcon(icon4);
			}
			if (n == 5) {
				icon5 = new ImageIcon("5.jpg");
				blockNameOrIcon.setIcon(icon5);
			}
			if (n == 6) {
				icon6 = new ImageIcon("6.jpg");
				blockNameOrIcon.setIcon(icon6);
			}
			if (n == 7) {
				icon7 = new ImageIcon("7.jpg");
				blockNameOrIcon.setIcon(icon7);
			}
			if (n == 8) {
				icon8 = new ImageIcon("8.jpg");
				blockNameOrIcon.setIcon(icon8);
			}
			else
				blockNameOrIcon.setText(" ");
		}
	}

	public void seeBlockNameOrIcon() {
		card.show(this, "view"); // 翻转到指定的组件
		validate();
	}

	public void seeBlockCover() {
		card.show(this, "cover");
		validate();
	}

	public JButton getBlockCover() {
		return blockCover;
	}
}

Block.java

package cn.itbaizhan.mine;

import javax.swing.ImageIcon;

public class Block {
	String name; // 名字,比如"雷"或数字
	int aroundMineNumber; // 周围雷的数目
	ImageIcon mineIcon; // 雷的图标
	boolean isMine = false; // 是否是雷
	boolean isMark = false; // 是否被标记
	boolean isOpen = false; // 是否被挖开

	public Block() {
		super();
	}

	public Block(String name, int aroundMineNumber, ImageIcon mineIcon, boolean isMine, boolean isMark,
			boolean isOpen) {
		super();
		this.name = name;
		this.aroundMineNumber = aroundMineNumber;
		this.mineIcon = mineIcon;
		this.isMine = isMine;
		this.isMark = isMark;
		this.isOpen = isOpen;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setAroundMineNumber(int n) {
		aroundMineNumber = n;
	}

	public int getAroundMineNumber() {
		return aroundMineNumber;
	}

	public void setMineIcon(ImageIcon icon) {
		mineIcon = icon;
	}

	public ImageIcon getMineicon() {
		return mineIcon;
	}

	public void setIsMine(boolean b) {
		isMine = b;
	}

	public boolean getIsMine() {
		return isMine;
	}

	public void setIsMark(boolean m) {
		isMark = m;
	}

	public boolean getIsMark() {
		return isMark;
	}

	public void setIsOpen(boolean p) {
		isOpen = p;
	}

	public boolean getIsOpen() {
		return isOpen;
	}

	@Override
	public String toString() {
		return "Block [name=" + name + ", aroundMineNumber=" + aroundMineNumber + ", mineIcon=" + mineIcon + ", isMine="
				+ isMine + ", isMark=" + isMark + ", isOpen=" + isOpen + "]";
	}
}

Record.java

package cn.itbaizhan.mine;

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

public class Record extends JDialog implements ActionListener {
	int time = 0;
	String grade = null;
	String key = null;
	String message = null;
	JTextField textName;
	JLabel label = null;
	JButton 确定, 取消;

	public Record() {
		setTitle("记录你的成绩");
		setBounds(300, 300, 240, 160);
		setResizable(false); // 设置用户是否可以调整大小
		setModal(true); // 设置窗口模式为对话框模式
		确定 = new JButton("确定");
		取消 = new JButton("取消");
		textName = new JTextField(8);
		textName.setText("匿名");
		确定.addActionListener(this);
		取消.addActionListener(this);
		setLayout(new GridLayout(2, 1));
		label = new JLabel("您现在是...高手,输入您的大名上榜");
		add(label);
		JPanel p = new JPanel();
		p.add(textName);
		p.add(确定);
		p.add(取消);
		add(p);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	}

	public void setGrade(String grade) {
		this.grade = grade;
		label.setText("您现在是" + grade + "高手,输入您的大名上榜");
	}

	public void setTime(int time) {
		this.time = time;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == 确定) {
			message = grade + "#" + time + "#" + " " + textName.getText();
			key = grade;
			writeRecord(key, message);
			setVisible(false);
		}
		if (e.getSource() == 取消) {
			setVisible(false);
		}
	}

	public void writeRecord(String key, String message) {
		File f = new File("英雄榜.txt");
		try {
			// 取出原有记录,更新记录
			FileInputStream in = new FileInputStream(f);
			ObjectInputStream object_in = new ObjectInputStream(in);
			Hashtable hashtable = (Hashtable) object_in.readObject();
			object_in.close();
			in.close();
			String temp = (String) hashtable.get(key);
			StringTokenizer fenxi = new StringTokenizer(temp, "#"); // 按分隔符分隔成一个个标记
			fenxi.nextToken(); // 返回下一个标记
			int n = Integer.parseInt(fenxi.nextToken());
			if (time < n) {
				hashtable.put(key, message);
				FileOutputStream out = new FileOutputStream(f);
				ObjectOutputStream object_out = new ObjectOutputStream(out);
				object_out.writeObject(hashtable);
				object_out.close();
				out.close();
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

ShowRecord.java

package cn.itbaizhan.mine;

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

public class ShowRecord extends JDialog implements ActionListener {
	File file = new File("英雄榜.txt");
	String name = null;
	Hashtable hashtable = null;
	JButton 显示, 重新记分;
	JLabel label初级[], label中级[], label高级[];

	public ShowRecord(JFrame frame, Hashtable h) {
		setTitle("扫雷榜");
		hashtable = h;
		setBounds(300, 300, 320, 185);
		setResizable(false);
		setVisible(false);
		setModal(true);
		label初级 = new JLabel[3];
		label中级 = new JLabel[3];
		label高级 = new JLabel[3];
		for (int i = 0; i < 3; i++) {
			label初级[i] = new JLabel();
			label初级[i].setBorder(null); // 设置无边框
			label中级[i] = new JLabel();
			label中级[i].setBorder(null);
			label高级[i] = new JLabel();
			label高级[i].setBorder(null);
		}
		label初级[0].setText("初级");
		label初级[1].setText("" + 999);
		label初级[1].setText("匿名");
		label中级[0].setText("中级");
		label中级[1].setText("" + 999);
		label中级[1].setText("匿名");
		label高级[0].setText("高级");
		label高级[1].setText("" + 999);
		label高级[1].setText("匿名");
		JPanel pCenter = new JPanel();
		pCenter.setLayout(new GridLayout(3, 3));
		for (int i = 0; i < 3; i++)
			pCenter.add(label初级[i]);
		for (int i = 0; i < 3; i++)
			pCenter.add(label中级[i]);
		for (int i = 0; i < 3; i++)
			pCenter.add(label高级[i]);
		pCenter.setBorder(BorderFactory.createTitledBorder("扫雷榜"));
		显示 = new JButton("最佳成绩");
		重新记分 = new JButton("重新记分");
		显示.addActionListener(this);
		重新记分.addActionListener(this);
		JPanel pSouth = new JPanel();
		pSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pSouth.add(重新记分);
		pSouth.add(显示);
		add(pCenter, BorderLayout.CENTER);
		add(pSouth, BorderLayout.SOUTH);
	}

	public void readAndShow() {
		try {
			FileInputStream in = new FileInputStream(file);
			ObjectInputStream object_in = new ObjectInputStream(in);
			hashtable = (Hashtable) object_in.readObject();
			object_in.close();
			in.close();
			String temp = (String) hashtable.get("初级");
			StringTokenizer fenxi = new StringTokenizer(temp, "#");
			label初级[0].setText(fenxi.nextToken());
			label初级[1].setText(fenxi.nextToken());
			label初级[2].setText(fenxi.nextToken());
			temp = (String) hashtable.get("中级");
			fenxi = new StringTokenizer(temp, "#");
			label中级[0].setText(fenxi.nextToken());
			label中级[1].setText(fenxi.nextToken());
			label中级[2].setText(fenxi.nextToken());
			temp = (String) hashtable.get("高级");
			fenxi = new StringTokenizer(temp, "#");
			label高级[0].setText(fenxi.nextToken());
			label高级[1].setText(fenxi.nextToken());
			label高级[2].setText(fenxi.nextToken());
		} catch (Exception e) {
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == 重新记分) {
			hashtable.put("初级", "初级#" + 999 + "#匿名");
			label初级[0].setText("初级");
			label初级[1].setText("" + 999);
			label初级[2].setText("匿名");
			hashtable.put("中级", "中级#" + 999 + "#匿名");
			label中级[0].setText("中级");
			label中级[1].setText("" + 999);
			label中级[2].setText("匿名");
			hashtable.put("高级", "高级#" + 999 + "#匿名");
			label高级[0].setText("高级");
			label高级[1].setText("" + 999);
			label高级[2].setText("匿名");
			try {
				FileOutputStream out = new FileOutputStream(file);
				ObjectOutputStream object_out = new ObjectOutputStream(out);
				object_out.writeObject(hashtable);
				object_out.close();
				out.close();
			} catch (IOException event) {
			}
			setVisible(false);
		}
		if (e.getSource() == 显示) {
			readAndShow();
		}
	}
}
经验分享 程序员 微信小程序 职场和发展