JAVA实现扫雷(myeclipse)
简单的目录
image文件夹里有扫雷中应用的图片,record是一个空白的文件夹,用来存放排行榜中的数据]
Block代码
package myFrame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class Block extends JPanel {
private MinePanel minePanel;
private int row; // 在雷区的行号
private int col; // 在雷区的列号
public final int WIDTH = 19; // 方块的宽度
public final int HEIGHT = 19; //方块的高度
private int type; //0,1,2,3,4,5,6,7,8,(9雷)
private int state; //0原始状态,1翻开,2标记为雷,3标记为问号
public static Toolkit tk;
public static final Image[] numberImage; //0~8
public static final Image[] flagImage ; //0标记为雷、1标记为问号
public static final Image[] bombImage; //0未爆炸、1已爆炸
public static final Image backImage; //未翻开时的背面
static{
tk = Toolkit.getDefaultToolkit();
numberImage = new Image[9];
flagImage = new Image[2];
bombImage = new Image[2];
for(int i=0; i<numberImage.length; i++){
String fileName = "image/"+i+".jpg";
numberImage[i] = tk.getImage(fileName);
}
for(int i=0; i<flagImage.length; i++){
String fileName = "image/flag"+i+".jpg";
flagImage[i] = tk.getImage(fileName);
}
for(int i=0; i<bombImage.length; i++){
String fileName = "image/bomb"+i+".jpg";
bombImage[i] = tk.getImage(fileName);
}
backImage = tk.getImage("Image/back.jpg");
}
public Block(MinePanel minePanel,int row, int col, int type, int state) {
super();
this.minePanel = minePanel;
this.row = row;
this.col = col;
this.type = type;
this.state = state;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
/**
* 翻开小方块
true:翻开成功 false:翻开失败
*/
public boolean open(){
if(type!=BlockType.MINE){
state = blockstate.OPEN;
draw(minePanel.getGraphics());
return true;
}
else{
state = blockstate.EXPLODED;
draw(minePanel.getGraphics());
return false;
}
}
/**
* 显示小方块
*/
public void draw(Graphics g){
int x = col*minePanel.GRID_WIDTH;
int y = row*minePanel.GRID_HEIGHT;
switch(state){
case blockstate.ORIGINAL:
g.drawImage(backImage,x,y, WIDTH,HEIGHT,minePanel);
break;
case blockstate.MINE_FLAG:
g.drawImage(flagImage[0],x, y, WIDTH,HEIGHT,minePanel);
break;
case blockstate.QUESTION_FLAG:
g.drawImage(flagImage[1],x, y, WIDTH,HEIGHT,minePanel);
break;
case +blockstate.OPEN:
if(type==BlockType.MINE){
g.drawImage(bombImage[0],x, y, WIDTH,HEIGHT,minePanel);
}
else{
g.drawImage(numberImage[type],x, y, WIDTH,HEIGHT,minePanel);
}
break;
case blockstate.EXPLODED:
if(type==BlockType.MINE){
g.drawImage(bombImage[1],x, y, WIDTH,HEIGHT,minePanel);
}
break;
}
}
}
blockstate代码
package myFrame;
public class blockstate {
static final int ORIGINAL=0;
static final int OPEN=1;
static final int MINE_FLAG=2;
static final int QUESTION_FLAG=3;
static final int EXPLODED=4;
}
BlockType代码
package myFrame;
public class BlockType {
static final int ZERO=0;//周围的雷数
static final int ONE=1;
static final int TWO=2;
static final int THERE=3;
static final int FOUR=4;
static final int FIVE=5;
static final int SIX=6;
static final int SEVEN=7;
static final int EIGHT=8;
static final int MINE=9;//是雷
}
DialogRecordName代码
package myFrame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DialogRecordName extends JDialog implements ActionListener{
private JLabel msg;
JTextField name;
private JButton OK;
private JButton cancel;
private int option=0;
public DialogRecordName(MineFrame parent,int second, int grade){
super(parent,"",JDialog.ModalityType.APPLICATION_MODAL);
switch(grade){
case Grade.LOWER:setTitle("初级成绩记录");
break;
case Grade.MEDIAL:setTitle("中级成绩记录");
break;
case Grade.HIGHER:setTitle("高级成绩记录");
break;
}
msg=new JLabel("你的成绩是"+second+"秒,破了记录,请输入名字。");
name=new JTextField(20);
OK=new JButton("确定");
cancel=new JButton("取消");
OK.addActionListener(this);
cancel.addActionListener(this);
this.setLayout(new GridLayout(2,1));
this.add(msg);
JPanel downPanel=new JPanel();
downPanel.add(name);
downPanel.add(OK);
downPanel.add(cancel);
this.add(downPanel);
this.pack();
this.setLocationRelativeTo(parent);
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(OK)){
option=1;
dispose();
}
else{
option=0;
dispose();
}
}
public int openDialog(){
this.setVisible(true);
return option;
}
}
DialogSelfDefine
package myFrame;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DialogSelfDefine extends JDialog implements ActionListener{
MineFrame mf;
JLabel labelRows=new JLabel("行数:");
JLabel labelCols=new JLabel("列数:");
JLabel labelMines=new JLabel("雷数");
JTextField tfRows=new JTextField(5);
JTextField tfCols=new JTextField(5);
JTextField tfMines=new JTextField(5);
JButton btOK=new JButton("确定");
JButton btCancel=new JButton("取消");
private int option=0;
public DialogSelfDefine(MineFrame parent){
super(parent,"用户自定义",JDialog.ModalityType.APPLICATION_MODAL);
mf=parent;
this.setLayout(new BorderLayout());
JPanel jpWest=new JPanel();
JPanel jpCenter=new JPanel();
JPanel jpSouth =new JPanel();
jpWest.setLayout(new GridLayout(3,1));
jpCenter.setLayout(new GridLayout(3,1));
jpSouth.setLayout(new FlowLayout());
jpWest.add(labelRows);
jpWest.add(labelCols);
jpWest.add(labelMines);
jpCenter.add(tfRows);
jpCenter.add(tfCols);
jpCenter.add(tfMines);
jpSouth.add(btOK);
jpSouth.add(btCancel);
tfRows.setText(""+mf.getRows());
tfCols.setText(""+mf.getCols());
tfMines.setText(""+mf.getMines());
this.add(new JPanel(),BorderLayout.NORTH);
this.add(jpWest,BorderLayout.WEST);
this.add(jpCenter,BorderLayout.CENTER);
this.add(new JPanel(),BorderLayout.EAST);
this.add(jpSouth,BorderLayout.SOUTH);
btOK.addActionListener(this);
btCancel.addActionListener(this);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(mf);
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(btOK)){
option=1;
dispose();
}
else if(e.getSource().equals(btCancel)){
option=0;
dispose();
}
}
public int openDialog(){
this.setVisible(true);
return option;
}
}
DialogShowRecord代码
package myFrame;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DialogShowRecord extends JDialog implements ActionListener{
MineFrame mf;
JLabel title=new JLabel(" ");
JLabel gradeLower=new JLabel("初级: ");
JLabel gradeMedial=new JLabel("中级: ");
JLabel gradeHigher=new JLabel("高级: ");
JLabel nameLower=new JLabel(" ");
JLabel nameMedial=new JLabel(" ");
JLabel nameHigher=new JLabel(" ");
JLabel scoreLower=new JLabel(" ");
JLabel scoreMedial=new JLabel(" ");
JLabel scoreHigher=new JLabel(" ");
JButton btOK=new JButton(" 确定");
private Record record;
public DialogShowRecord(MineFrame parent){
super(parent,"扫雷排行榜",JDialog.ModalityType.APPLICATION_MODAL);
this.mf=parent;
this.setLayout(new BorderLayout());
JPanel jpCenter=new JPanel();
JPanel jpSouth=new JPanel();
JPanel jpNorth=new JPanel();
jpNorth.add(title);
jpCenter.setLayout(new GridLayout(3,3));
jpCenter.add(gradeLower);
jpCenter.add(nameLower);
jpCenter.add(scoreLower);
jpCenter.add(gradeMedial);
jpCenter.add(nameMedial);
jpCenter.add(scoreMedial);
jpCenter.add(gradeHigher);
jpCenter.add(nameHigher);
jpCenter.add(scoreHigher);
jpSouth.add(btOK);
this.add(title,BorderLayout.NORTH);
this.add(jpCenter,BorderLayout.CENTER);
this.add(btOK,BorderLayout.SOUTH);
btOK.addActionListener(this);
Record record =mf.getRecord();
if(record!=null){
title.setText("扫雷最好的记录是:");
nameLower.setText(record.getLowerName());
nameMedial.setText(record.getMedialName());
nameHigher.setText(record.getHigherName());
scoreLower.setText(" "+record.getLowerScore());
scoreMedial.setText(" "+record.getMedialScore());
scoreHigher.setText(" "+record.getHigherScore());
}
else{
title.setText("未找到扫雷记录");
}
this.setSize(300, 150);
this.setLocationRelativeTo(parent);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0){
dispose();
}
}
Grade代码
package myFrame;
public class Grade {
public static final int LOWER=1;
public static final int MEDIAL=2;
public static final int HIGHER=3;
public static final int SELF_DEFINE=4;
}
MineFrame
package myFrame;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MineFrame extends JFrame {
JMenuBar menuBar;
JMenu menu;
JMenuItem[] menuItems;
String[] menuItemNames = {"初级","中级","高级","自定义","排行榜","退出"};
JTextField mineRemained; //显示剩余雷数的文本框
JButton reStart;
JTextField timeUsed;
Icon face;
JPanel upPanel;
MinePanel minePanel;
private int rows;
private int cols;
private int mines;
private int grade;
private Record record;
private boolean gamming;
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getCols() {
return cols;
}
public void setCols(int cols) {
this.cols = cols;
}
public int getMines() {
return mines;
}
public void setMines(int mines) {
this.mines = mines;
}
private boolean stoped;
public MineFrame(){
createMenu();
createUpPanel();
initParameter(10,10,10);
initRecord();
Container c = this.getContentPane();//Container容器
c.add(upPanel,BorderLayout.NORTH);
minePanel = new MinePanel(this,rows,cols,mines);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
centerPanel.add(new JPanel(),BorderLayout.WEST);
centerPanel.add(minePanel,BorderLayout.CENTER);
centerPanel.add(new JPanel(),BorderLayout.EAST);
centerPanel.add(new JPanel(),BorderLayout.SOUTH);
c.add(centerPanel,BorderLayout.CENTER);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);//相对位置
this.setResizable(true);//可调整大小
this.setVisible(true);//可见
}
private void createMenu(){
menuBar = new JMenuBar();
menu = new JMenu("游戏");
menuItems = new JMenuItem[menuItemNames.length];
for(int i = 0;i<menuItemNames.length;i++){
menuItems[i] = new JMenuItem(menuItemNames[i]);
menu.add(menuItems[i]);
}
menuBar.add(menu);
this.setJMenuBar(menuBar);
MenuMonitor mm = new MenuMonitor();
for(int i = 0;i<menuItems.length;i++){
menuItems[i].addActionListener(mm);
}
}
class ButtonMonitor implements ActionListener{
public void actionPerformed(ActionEvent e){
if(minePanel.time!=null)
minePanel.time.cancel();
initParameter(rows,cols,mines);
minePanel.initMinePanel(rows, cols, mines);
minePanel.repaint();
}
}
class MenuMonitor implements ActionListener{
public void actionPerformed(ActionEvent e){
int c1=cols;
int r1=rows;
JMenuItem mi = (JMenuItem)e.getSource();
if(mi.equals(menuItems[5])){//退出
System.exit(0);
}
else if(mi.equals(menuItems[4])){
DialogShowRecord dlg=new DialogShowRecord(MineFrame.this);
}
else{
if(mi.equals(menuItems[0])){
grade=Grade.LOWER;
initParameter(10,10,10);
}
else if(mi.equals(menuItems[1])){
grade=Grade.MEDIAL;
initParameter(15,15,30);
}
else if(mi.equals(menuItems[2])){
grade=Grade.HIGHER;
initParameter(20,20,40);
}
else if(mi.equals(menuItems[3])){
DialogSelfDefine dlg=new DialogSelfDefine(MineFrame.this);
if(dlg.openDialog()==1){
grade=Grade.SELF_DEFINE;
int r=Integer.valueOf(dlg.tfRows.getText());
int c=Integer.valueOf(dlg.tfCols.getText());
int m=Integer.valueOf(dlg.tfMines.getText());
initParameter(r,c,m);
}
}
minePanel.initMinePanel(rows, cols, mines);
if(r1!=rows||c1!=cols){
pack();
}
MineFrame.this.setLocationRelativeTo(null);
}
}
}
private void createUpPanel(){
mineRemained = new JTextField("0000");
mineRemained.setEditable(false);
timeUsed = new JTextField("0000");
timeUsed.setEditable(false);
face = new ImageIcon("image/smile.jpg");
reStart = new JButton(face);
reStart.addActionListener(new ButtonMonitor());
JPanel center = new JPanel();
JPanel right = new JPanel();
JPanel left = new JPanel();
center.add(reStart);
left.add(mineRemained);
right.add(timeUsed);
upPanel = new JPanel(new BorderLayout());
upPanel.add(left,BorderLayout.WEST);
upPanel.add(center,BorderLayout.CENTER);
upPanel.add(right,BorderLayout.EAST);
}
private void initParameter(int rows,int cols,int mines){
this.rows = rows;
this.cols =cols;
this.mines = mines;
stoped=false;
gamming=false;
setStoped(false);
setGamming(false);
setTimeUsed(0);
setMinesRemained(mines);
}
public boolean isGamming() {
return gamming;
}
public void setGamming(boolean gamming) {
this.gamming = gamming;
}
public boolean isStoped() {
return stoped;
}
public void setStoped(boolean stoped) {
this.stoped = stoped;
}
public void setMinesRemained(int mines){
String strMines;
if(mines>9999){
strMines="9999";
}
else if(mines/10==0){
strMines="000"+mines;
}
else if(mines/100==0){
strMines="00"+mines;
}
else if(mines/1000==0){
strMines="0"+mines;
}
else{
strMines=""+mines;
}
mineRemained.setText(strMines);
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public void setTimeUsed(int second) {
String strSecond;
if(second>9999){
strSecond="9999";
}
else if(second/10==0){
strSecond="000"+second;
}
else if(second/100==0){
strSecond="00"+second;
}
else if(second/1000==0){
strSecond="0"+second;
}
else{
strSecond=""+second;
}
timeUsed.setText(strSecond);
// TODO Auto-generated method stub
}
public Record getRecord() {
return record;
}
public void setRecord(Record record) {
this.record = record;
}
public void initRecord(){
grade=Grade.LOWER;
RecordDao rd=new RecordDao();
record =rd.readRecord();
if(record==null){
record=new Record();
record.setLowerName("匿名");
record.setLowerScore(9999);
record.setMedialName("匿名");
record.setMedialScore(9999);
record.setHigherName("匿名");
record.setHigherScore(9999);
}
}
}
MinePanel代码
package myFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.color.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MinePanel extends JPanel{
public final int GRID_WIDTH = 20; //方格宽度
public final int GRID_HEIGHT = 20;//方格高度
private MineFrame mf;
private int cols; //雷区列数
private int rows;//雷区行数
private int mines;//雷区雷数
private int remainedMines;//未标记的雷数
private int openedBlocks;//已经翻开的雷数
private Block[][]blocks;//方块数组
UpdateTimeTask time;
public MinePanel(MineFrame mf,int rows,int cols,int mines){
this.mf=mf;
initMinePanel(rows,cols,mines);
this.addMouseListener(new MouseMonitor());
this.setBackground(new Color(210,210,210));
}
/*初始化雷区*/
public void initMinePanel(int rows,int cols,int mines){
this.cols = cols;
this.rows = rows;
this.mines = mines;
remainedMines = mines;
openedBlocks = 0;
createBlocks();
layMines();
repaint();
}
public void createBlocks(){
blocks = new Block[rows][cols];
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){
blocks[i][j] = new Block(this,i,j,BlockType.ZERO,blockstate.ORIGINAL);
}
}
}
/*随机布雷*/
private void layMines(){
int r;
int c;
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){
blocks[i][j].setType(BlockType.ZERO);
blocks[i][j].setState(blockstate.ORIGINAL);
}
}
//随机布雷
int m = 0;
while(m<mines){
r = (int)(Math.random()*rows);
c = (int)(Math.random()*cols);
if(blocks[r][c].getType()!=BlockType.MINE){
blocks[r][c].setType(BlockType.MINE);
m++;
}
}
//计算每个点开方块周围的雷数
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){
if(blocks[i][j].getType()!=BlockType.MINE){
countMines(i,j);
}
}
}
}
/* 计算小方块周围的雷数*/
private void countMines(int row, int col) {
int mineNumber = 0;
for (int i = row - 1; i <= row + 1; i++) {
if ((i >= 0) && (i < rows)) {
for (int j = col - 1; j <= col + 1; j++) {
if ((j >= 0) && (j < cols)){
if (blocks[i][j].getType() == BlockType.MINE) {
mineNumber++;
}
}
}
}
}
blocks[row][col].setType(mineNumber);
}
public void paint(Graphics g){
super.paint(g);
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){
blocks[i][j].draw(g);
}
}
}
public Dimension getPreferredSize(){
return new Dimension(cols*GRID_WIDTH,rows*GRID_HEIGHT);
}
/*监听鼠标消息*/
public class MouseMonitor extends MouseAdapter {
public void mouseClicked(MouseEvent event){
int col=event.getX()/GRID_WIDTH;
int row=event.getY()/GRID_HEIGHT;
if(mf.isStoped())
return;
if(!mf.isGamming()){
mf.setGamming(true);
mf.setGamming(true);
time=new UpdateTimeTask(mf);
Timer timer =new Timer();
timer.schedule(time,1000,1000);
}
if(event.getButton()==MouseEvent.BUTTON1){
open(row,col);
}
else if(event.getButton() == MouseEvent.BUTTON3){
if(blocks[row][col].getState()==blockstate.ORIGINAL){
blocks[row][col].setState(blockstate.MINE_FLAG);
remainedMines--;
mf.setMinesRemained(remainedMines);
blocks[row][col].draw(MinePanel.this.getGraphics());
}
else if(blocks[row][col].getState()==blockstate.MINE_FLAG){
blocks[row][col].setState(blockstate.QUESTION_FLAG);
remainedMines++;
mf.setMinesRemained(remainedMines);
blocks[row][col].draw(MinePanel.this.getGraphics());
}
/*快速扫雷*/
else if(blocks[row][col].getState()==blockstate.QUESTION_FLAG){
blocks[row][col].setState(blockstate.ORIGINAL);
blocks[row][col].draw(MinePanel.this.getGraphics());
}
else if(blocks[row][col].getState()==blockstate.OPEN){
int flagNumber=0;
for(int i=row-1;i<=row+1;i++){
for(int j=col-1;j<=col+1;j++){
if((i>=0)&&(i<rows)&&(j<cols)&&(j>=0)){
if(blocks[i][j].getState()==blockstate.MINE_FLAG){
flagNumber++;
}
}
}
}
if(flagNumber==blocks[row][col].getType()){
for(int i=row-1;i<=row+1;i++){
for(int j=col-1;j<=col+1;j++){
if((i>=0)&&(i<rows)&&(j>=0)&&(j<cols)){
open(i,j);
}
}
}
}
}
}
}
}
public void open(int row, int col){
if(blocks[row][col].getState()==blockstate.ORIGINAL){
if(blocks[row][col].open()){
openedBlocks++;
if(openedBlocks == rows * cols - mines){
wins(); //扫雷成功
}
if(blocks[row][col].getType()==BlockType.ZERO){
search(row, col);
}
}else{
lose(row,col); //扫雷失败
}
}
}
//失败
private void lose(int row,int col) {
time.cancel();
int i,j;
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if( (blocks[i][j].getType()==BlockType.MINE) && (blocks[i][j].getState()!=blockstate.MINE_FLAG)){
blocks[i][j].setState(blockstate.OPEN);
blocks[i][j].draw(MinePanel.this.getGraphics());
}
}
}
blocks[row][col].setState(blockstate.EXPLODED);
blocks[row][col].draw(MinePanel.this.getGraphics());
mf.setGamming(false);
mf.setStoped(true);
JOptionPane.showMessageDialog(this, "抱歉,扫雷失败!");
}
//快速排雷
private void search(int row, int col){
int i,j;
for(i=row-1; i<=row+1; i++){
if( (i<0) || (i>=rows) ){
continue;
}
for(j=col-1; j<=col+1; j++){
if( (j<0)||(j>=cols) ){
continue;
}
if( blocks[i][j].getState()==blockstate.ORIGINAL ){
blocks[i][j].open();
openedBlocks++;
if(openedBlocks == rows * cols - mines){
wins();
}
if(blocks[i][j].getType()==BlockType.ZERO){
search(i,j);
}
}
}
}
}
//成功
public void wins() {
int second =time.getSecond();
time.cancel();
mf.setGamming(false);
mf.setStoped(true);
JOptionPane.showMessageDialog(this, "恭喜,扫雷成功!");
int grade =mf.getGrade();
Record record=mf.getRecord();
boolean newRecord=false;
switch(grade){
case Grade.LOWER:
if(second<record.getLowerScore()){
newRecord=true;
}
break;
case Grade.MEDIAL:
if(second<record.getMedialScore()){
newRecord=true;
}
break;
case Grade.HIGHER:
if(second<record.getHigherScore()){
newRecord=true;
}
break;
}
if(newRecord){
String name=null;
DialogRecordName dlg=new DialogRecordName(mf,second,grade);
int option=dlg.openDialog();
if(option==1){
name=dlg.name.getText();
switch(grade){
case Grade.LOWER:
record.setLowerName(name);
record.setLowerScore(second);
break;
case Grade.MEDIAL:
record.setMedialName(name);
record.setMedialScore(second);
break;
case Grade.HIGHER:
record.setHigherName(name);
record.setHigherScore(second);
break;
}
RecordDao rd =new RecordDao();
rd.writeRecord(record);
}
}
}
}
Record代码
package myFrame;
import java.io.Serializable;
public class Record implements Serializable {
private static final long serialVersionUID=91;
public int getLowerScore() {
return lowerScore;
}
public void setLowerScore(int lowerScore) {
this.lowerScore = lowerScore;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
private String higherName;
private int higherScore;
private String medialName;
private int medialScore;
private String lowerName;
private int lowerScore;
public Record(){
}
public Record(String higherName,int higherScore,String mediaName,
int mediaScore,String lowerName,int lowerScore){
this.higherName=higherName;
this.higherScore=higherScore;
this.medialName=medialName;
this.medialScore=mediaScore;
this.lowerName=lowerName;
this.lowerScore=lowerScore;
}
public String getHigherName() {
return higherName;
}
public void setHigherName(String higherName) {
this.higherName = higherName;
}
public int getHigherScore() {
return higherScore;
}
public void setHigherScore(int higherScore) {
this.higherScore = higherScore;
}
public String getMedialName() {
return medialName;
}
public void setMedialName(String medialName) {
this.medialName = medialName;
}
public int getMedialScore() {
return medialScore;
}
public void setMedialScore(int medialScore) {
this.medialScore = medialScore;
}
public String getLowerName() {
return lowerName;
}
public void setLowerName(String lowerName) {
this.lowerName = lowerName;
}
public int getLowerScose() {
return lowerScore;
}
public void setLowerScose(int lowerScose) {
this.lowerScore = lowerScose;
}
}
RecordDao代码
package myFrame;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class RecordDao {
private String filename;
public RecordDao(){
filename="record/record.dat";
}
public Record readRecord(){
FileInputStream fis=null;
ObjectInputStream ois=null;
Record record=null;
try {
fis=new FileInputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return null;
}
try {
ois=new ObjectInputStream(fis);
record=(Record)ois.readObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return record;
}
public void writeRecord(Record record){
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try {
fos=new FileOutputStream(filename);
oos=new ObjectOutputStream(fos);
oos.writeObject(record);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
saolei代码
package myFrame;
public class saolei {
public static void main(String[] args) {
MineFrame mineFrame=new MineFrame();
}
}
UpdateTimeTask
package myFrame;
import java.util.TimerTask;
public class UpdateTimeTask extends TimerTask{
private MineFrame mf;
private int second;
public UpdateTimeTask(MineFrame mf){
super();
this.mf=mf;
this.second=0;
}
public MineFrame getMf() {
return mf;
}
public void setMf(MineFrame mf) {
this.mf = mf;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
public void run(){
second++;
mf.setTimeUsed(second);
}
}
上面为所有的代码,图片就不往上面发了,根据要求存放就可以了,名字一定按下面的形式写!
下面为效果图