📄 mine.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.event.*;
class MineFrame extends JFrame{
static SingleMine sm;
static int gridx,gridy,mines;
static int level;
JMenuBar mb = new JMenuBar();
public MineFrame(int x,int y,int m,int lv){
super();
//显示开始对话框
MineDialog md=new MineDialog(x,y,m,lv);
//获取参数
gridx=md.gridx;
gridy=md.gridy;
mines=md.mines;
level=md.level;
md.dispose();
//设置菜单
JMenu gameMenu = new JMenu("游戏");
JMenuItem selMI = new JMenuItem("新游戏...");
JMenuItem tjMI = new JMenuItem("扫雷英雄榜");
gameMenu.add(selMI);
gameMenu.add(tjMI);
selMI.addActionListener(new newGameClick());
tjMI.addActionListener(new rankDlgClick());
gameMenu.addMenuListener(new menuOpen());
mb.add(gameMenu);
setJMenuBar(mb);
//设置窗体参数
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new Mine_Close());
setTitle("扫雷");
setSize(SingleMine.getDimension(gridx,gridy));
setLocation(300,200);
setVisible(true);
setResizable(false);
//选择载入游戏
if(level==5){
File sav=new File("saveInfo.dat");
try{
//从 saveInfo.dat 读入参数
DataInputStream in=new DataInputStream(new FileInputStream(sav));
gridx=in.readInt();
gridy=in.readInt();
mines=in.readInt();
level=in.readInt();
in.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"读取失败!");
System.exit(1);
}
//删除存档
sav.delete();
sm=new SingleMine(gridx,gridy,mines,getContentPane(),level);
//读入游戏数据
sm.loadGame();
}
else
sm=new SingleMine(gridx,gridy,mines,getContentPane(),level);
getContentPane().addMouseListener(sm);
getContentPane().addMouseMotionListener(sm);
}
class newGameClick implements ActionListener{
public void actionPerformed(ActionEvent e){
MineFrame m;
if(sm.GAME_RUN==1){
sm.timer.stop();
int ret=JOptionPane.showConfirmDialog(null,"游戏正在运行,是否保存?","扫雷",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
switch(ret){
case JOptionPane.YES_OPTION:
saveGame();
m=new MineFrame(gridx,gridy,mines,level);
dispose();
break;
case JOptionPane.NO_OPTION:
m=new MineFrame(gridx,gridy,mines,level);
dispose();
break;
case JOptionPane.CANCEL_OPTION:
case JOptionPane.CLOSED_OPTION:
sm.timer.start();
}
}
else{
m=new MineFrame(gridx,gridy,mines,level);
dispose();
}
}
}
class rankDlgClick implements ActionListener{
public void actionPerformed(ActionEvent e){
tjDialog mm=new tjDialog(-1,-1);
}
}
class menuOpen implements MenuListener{
public void menuCanceled(MenuEvent e){
sm.reloadMap();
if(sm.timer!=null && sm.GAME_RUN==1) sm.timer.start();
}
public void menuDeselected(MenuEvent e){
sm.reloadMap();
if(sm.timer!=null && sm.GAME_RUN==1) sm.timer.start();
}
public void menuSelected(MenuEvent e){
if(sm.timer!=null && sm.GAME_RUN==1) sm.timer.stop();
}
}
class Mine_Close implements WindowListener{
public void windowOpened(WindowEvent e){}
public void windowClosing(WindowEvent e){
if(sm.GAME_RUN==1){
sm.timer.stop();
int ret=JOptionPane.showConfirmDialog(null,"游戏正在运行,是否保存?","扫雷",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
switch(ret){
case JOptionPane.YES_OPTION:
saveGame();
System.exit(0);
break;
case JOptionPane.NO_OPTION:
System.exit(0);
break;
case JOptionPane.CANCEL_OPTION:
case JOptionPane.CLOSED_OPTION:
sm.timer.start();
}
}
else{
System.exit(0);
}
}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){
if(sm!=null && sm.GAME_RUN==1 && sm.timer!=null && sm.md!=null){
sm.md.update();
sm.timer.start();
}
}
public void windowDeactivated(WindowEvent e){
if(sm!=null && sm.GAME_RUN==1 && sm.timer!=null && sm.md!=null) sm.timer.stop();
}
}
public void paint(Graphics g){
super.paint(g);
if(sm!=null)
sm.md.update();
}
public void saveGame(){
try{
DataOutputStream out=new DataOutputStream(new FileOutputStream("saveInfo.dat"));
out.writeInt(gridx);
out.writeInt(gridy);
out.writeInt(mines);
out.writeInt(level);
out.close();
sm.saveGame();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"保存失败!");
}
}
}
public class Mine{
public static void main(String[] args){
MineFrame m=new MineFrame(20,20,50,4);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -