📄 bumb.java
字号:
import java.io.*;
import sun.audio.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
class Bomb extends JFrame implements ActionListener ,MouseListener, Runnable,KeyListener
{
int position[][]=new int[9][9];
JPanel panel1,panel2;
JButton b;//具有表情的标签按钮
JLabel label[]=new JLabel[81];//定义雷区的按钮
boolean haveFlag[]=new boolean[81];//用来判断雷是否被插上小旗
boolean haveSign[]=new boolean[81];
boolean isAlive[][]=new boolean[9][9];//判断标签是否处于活动状态
boolean sign;//用来判断是否需要标记
boolean haveSound;//用来判段是否需要声音
boolean win;//用来判断是否获胜
JLabel label1=new JLabel("");//设置雷数的标签
JLabel label2=new JLabel("");//设置扫雷时间的标签
Thread timeThread;//用来计算扫雷花的时间的线程
int time;//用来计算扫雷花的时间
JMenuBar mb;
JMenu m1,m21;
JMenuItem m2,m9,m0,m22,m23,m24,m25;
JCheckBoxMenuItem m3,m4,m5,m6,m7,m8,ms;
JTextArea ta;//扫雷英雄版上的文本域;
JFrame h;//扫雷英雄版的框架
JFrame fm;//打破记录的框架
JTextField t2;//记录打破记录人的名字的文本框
int second;//记录打破记录人花的时间
public Bomb()
{
JFrame f=new JFrame("扫雷");//创建框架
f.getContentPane().setLayout(null);//自定义框架的设置
panel1=new JPanel();//将panel1中的布局结构设置为FlowLayout
panel1.setLayout(null);//自定义框架的设置
panel2=new JPanel(new GridLayout(9,9,-10,-10));//将panel中的布局结构设置为9行9列的网格状
panel1.setSize(170,40);//设置panel长为170个像素高为40个像素
panel2.setSize(170,180);//设置panel2长为170个像素高为200个像素
f.getContentPane().add(panel1);
f.getContentPane().add(panel2);
panel2.setLocation(0,40);
f.setSize(170,270);
for(int i=0;i<81;i++)//初定义所有的雷上没有小旗
haveFlag[i]=false;
for(int i=0;i<81;i++)//初定义所有的雷上没有标记
haveSign[i]=false;
start();//随机产生雷的位置 并且初始化一些数据数组
/*************************************************************************/ //添加菜单
mb=new JMenuBar();
m1=new JMenu("游戏(G)");
m1.setMnemonic(KeyEvent.VK_G);//添加文件菜单项
m2=new JMenuItem("开局(N) F1");//创建开局菜单项
m2.setMnemonic(KeyEvent.VK_N);
m3=new JCheckBoxMenuItem("初级(B)",true);//创建复选初级菜单项
m3.setMnemonic(KeyEvent.VK_B);
m4=new JCheckBoxMenuItem("中级(i)",false);//创建复选中级菜单项
m4.setMnemonic(KeyEvent.VK_I);
m5=new JCheckBoxMenuItem("高级(E)",false);//创建复选高级菜单项
m5.setMnemonic(KeyEvent.VK_E);
m6=new JCheckBoxMenuItem("自定义(C)",false);//创建复选自定义菜单项
m6.setMnemonic(KeyEvent.VK_C);
m7=new JCheckBoxMenuItem("标记(?)(M)",false);//创建复选标记菜单项
m7.setMnemonic(KeyEvent.VK_M);
m8=new JCheckBoxMenuItem("颜色(L)",true);//创建复选颜色菜单项
m8.setMnemonic(KeyEvent.VK_L);
ms=new JCheckBoxMenuItem("声音(s)",false);
ms.setMnemonic(KeyEvent.VK_S);
m9=new JMenuItem("扫雷英雄排行榜(T)");//创建排行榜菜单选项
m9.setMnemonic(KeyEvent.VK_T);
m0=new JMenuItem("退出(X)");//创建退出菜单选项
m0.setMnemonic(KeyEvent.VK_X);
m21=new JMenu("帮助(H)");//创建帮助菜单选项
m21.setMnemonic(KeyEvent.VK_H);
m22=new JMenuItem("目录(C) F1");//创建目录菜单选项
m22.setMnemonic(KeyEvent.VK_C);
m23=new JMenuItem("使用帮助主题(S)...");//创建主题菜单选项
m23.setMnemonic(KeyEvent.VK_S);
m24=new JMenuItem("使用帮助(H)");//创建帮助菜单选项
m24.setMnemonic(KeyEvent.VK_H);
m25=new JMenuItem("关于扫雷(A)");//创建关于扫雷菜单选项
m25.setMnemonic(KeyEvent.VK_A);
m1.add(m2);//往菜单栏中添加菜单选项
m1.addSeparator();
m1.add(m3);
m1.add(m4);
m1.add(m5);
m1.add(m6);
m1.addSeparator();
m1.add(m7);
m1.add(m8);
m1.add(ms);
m1.addSeparator();
m1.add(m9);
m1.add(m0);
m21.add(m22);
m21.add(m23);
m21.add(m24);
m21.addSeparator();
m21.add(m25);
mb.add(m1);//将菜单项目添加到菜单条
mb.add(m21);
f.setJMenuBar(mb);//将菜单添加到框架
m2.addActionListener(this);//添加菜单的监听器
m3.addActionListener(this);
m4.addActionListener(this);
m5.addActionListener(this);
m6.addActionListener(this);
m7.addActionListener(this);
m8.addActionListener(this);
ms.addActionListener(this);
m9.addActionListener(this);
m0.addActionListener(this);
m22.addActionListener(this);
m23.addActionListener(this);
m24.addActionListener(this);
m25.addActionListener(this);
//************************************************************************************//定义菜单
panel1.add(label1);
label1.setSize(50,35);
label1.setLocation(0,0);
label1.setForeground(Color.red);//标签的前景色为红色
label1.setFont(new Font("Arial Black",Font.PLAIN,24));
label1.setText("010");//雷的初始个数为10个
panel1.add(label2);
label2.setSize(50,35);
label2.setLocation(113,0);
label2.setForeground(Color.green);//标签的前景色为红色
label2.setFont(new Font("Arial Black",Font.PLAIN,24));
label2.setText("000");//时间初始值为为0秒
ImageIcon image1=new ImageIcon("image/smile.jpg");//定义Jbutton类型的图标按钮
b=new JButton(image1);
panel1.add(b);
b.setSize(25,25);
b.setLocation(67,7);
b.addActionListener(this);//注册按钮图标的监听器
ImageIcon image2=new ImageIcon("image/gray.jpg");
for(int i=0 ;i<81;i++)
{
label[i]=new JLabel(image2);
panel2.add(label[i]);
label[i].addMouseListener(this);//注册label的监听器
}
f.setVisible(true);
}
//*******************************************************************************************//对actionListener接口进行定义
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("退出(X)"))//选择菜单栏中的退出按钮退出程序
{
System.exit(0);
}
if(e.getSource()==b)//点击标签按钮重新开始游戏
{
start();
ImageIcon smile=new ImageIcon("image/smile.jpg");
b.setIcon(smile);
for( int i=0;i<81;i++)//添加监听器
{
ImageIcon gray=new ImageIcon("image/gray.jpg");
label[i].setText("");
label[i].setIcon(gray);
label[i].removeMouseListener(this);
label[i].addMouseListener(this);
}
}
if(e.getActionCommand()=="开局(N) F1")//选择菜单栏中的开局选项开始游戏
{
start();
ImageIcon smile=new ImageIcon("image/smile.jpg");
for( int i=0;i<81;i++)//添加监听器
{
ImageIcon gray=new ImageIcon("image/gray.jpg");
label[i].setText("");
label[i].setIcon(gray);
label[i].removeMouseListener(this);
label[i].addMouseListener(this);
}
b.setIcon(smile);
}
if(e.getActionCommand()=="初级(B)")//选择菜单栏中的初级选项开始初级游戏
{
start();
ImageIcon smile=new ImageIcon("image/smile.jpg");
b.setIcon(smile);
for( int i=0;i<81;i++)//添加监听器
{
ImageIcon gray=new ImageIcon("image/gray.jpg");
label[i].setText("");
label[i].setIcon(gray);
label[i].removeMouseListener(this);
label[i].addMouseListener(this);
}
m3.setState(true);
m4.setState(false);
m6.setState(false);
m7.setState(false);
}
if(e.getActionCommand()=="标记(?)(M)") //选取是否需要标记
{
if(sign==false)
{
sign=true;
}
else
{
sign=false;
}
}
if(e.getActionCommand()=="声音(s)")
{
if(haveSound==false)
{
haveSound=true;
}
else
{
haveSound=false;
}
}
if(e.getActionCommand()=="扫雷英雄排行榜(T)")
{
String name1="",name2="",name3="";
String mark1="",mark2="",mark3="";
BufferedReader br;
try
{
br=new BufferedReader(new FileReader("data.txt"));
name1=br.readLine();
mark1=br.readLine();
name2=br.readLine();
mark2=br.readLine();
name3=br.readLine();
mark3=br.readLine();
br.close();
}
catch(FileNotFoundException f)
{
}
catch(IOException i)
{
}
String data="\n"+" " +"初级: "+mark1+" 秒"+" "+name1+
"\n"+" " +"中级: "+mark2+" 秒"+" "+name2+
"\n"+" " +"高级: "+mark3+" 秒"+" "+name3;
h=new JFrame("扫雷英雄版");
h.setLayout(null);
h.setSize(265,150);
JButton b1=new JButton("重新计分");
b1.addActionListener(this);
JButton b2=new JButton("确定");
b2.addActionListener(this);
ta=new JTextArea();
ta.setSize(265,80);
b1.setSize(90,20);
b2.setSize(60,20);
ta.setText(data);
h.add(ta,"North");
h.add(b1);
b1.setLocation(40,80);
b2.setLocation(180,80);
h.add(b2);
h.setVisible(true);
}
if(e.getActionCommand()=="确定")//选择扫雷英雄版上的确定按钮
{
h.setVisible(false);
}
if(e.getActionCommand()=="重新计分")//选择扫雷英雄版上的重新计分按钮
{
String data="\n"+" " +"初级: "+"999 秒"+" 匿名"+
"\n"+" " +"中级: "+"999 秒"+" 匿名"+
"\n"+" " +"高级: "+"999 秒"+" 匿名";
ta.setText(data);
BufferedWriter bw;
try
{
bw=new BufferedWriter(new FileWriter("data.txt"));
bw.write("匿名");
bw.newLine();
bw.write("999");
bw.newLine();
bw.write("匿名");
bw.newLine();
bw.write("999");
bw.newLine();
bw.write("匿名");
bw.newLine();
bw.write("999");
bw.close();
}
catch(FileNotFoundException f)
{
}
catch(IOException i)
{
}
}
if(e.getActionCommand()=="确定 ")
{
fm.setVisible(false);
String s=t2.getText();
BufferedWriter bw;
try
{
bw=new BufferedWriter(new FileWriter("data.txt"));
bw.write(s);
bw.newLine();
bw.write(String.valueOf(second));
bw.newLine();
bw.write("匿名");
bw.newLine();
bw.write("999");
bw.newLine();
bw.write("匿名");
bw.newLine();
bw.write("999");
bw.close();
}
catch(FileNotFoundException f)
{
}
catch(IOException f)
{
}
String name1="",name2="",name3="";
String mark1="",mark2="",mark3="";
BufferedReader br;
try
{
br=new BufferedReader(new FileReader("data.txt"));
name1=br.readLine();
mark1=br.readLine();
name2=br.readLine();
mark2=br.readLine();
name3=br.readLine();
mark3=br.readLine();
br.close();
}
catch(FileNotFoundException f)
{
}
catch(IOException i)
{
}
String data="\n"+" " +"初级: "+mark1+" 秒"+" "+name1+
"\n"+" " +"中级: "+mark2+" 秒"+" "+name2+
"\n"+" " +"高级: "+mark3+" 秒"+" "+name3;
h=new JFrame("扫雷英雄版");
h.setLayout(null);
h.setSize(265,150);
JButton b1=new JButton("重新计分");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -