用java实现java扫雷游戏视频教程,最简单的,运行以后只生成地雷阵就行

18231人阅读
java(17)
package com.test.
import java.awt.event.ActionE
import java.awt.event.ActionL
import java.awt.event.MouseE
import java.awt.event.MouseL
import javax.swing.JB
import javax.swing.JF
import javax.swing.JL
* 这个是一个简单的扫雷例子,刚接触swing编写的,适合新手练习
* 该程序使用setBounds(x,y,w,h)对控件布局
* 做法参考win xp自带的扫雷,当然还写功能没做出来,
* 另外做出来的功能有些还存在bug
* @author Ping_QC
public class Test extends JFrame implements ActionListener, Runnable,
MouseListener {
private static final long serialVersionUID = -5039613L;
private final int EMPTY
private final int MINE
private final int CHECKED
private final int MINE_COUNT
= 10; // 雷的个数
private final int BUTTON_BORDER = 50; // 每个点的尺寸
private final int MINE_SIZE
= 10; // 界面规格, 20x20
private final int START_X
= 20; // 起始位置x
private final int START_Y
= 50; // 起始位置y
private JButton[][]
private JL
private JLabel showT
private int[][]
* 检测某点周围是否有雷,周围点的坐标可由该数组计算得到
private int[][] mv = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 },
{ 1, -1 }, { 0, -1 }, { -1, -1 } };
* 随机产生设定个数的雷
public void makeMine() {
int i = 0, tx,
for (; i & MINE_COUNT;) {
tx = (int) (Math.random() * MINE_SIZE);
ty = (int) (Math.random() * MINE_SIZE);
if (map[tx][ty] == EMPTY) {
map[tx][ty] = MINE;
i++; // 不记重复产生的雷
* 将button数组放到frame上,与map[][]数组对应
public void makeButton() {
for (int i = 0; i & MINE_SIZE; i++) {
for (int j = 0; j & MINE_SIZE; j++) {
jb[i][j] = new JButton();
// if (map[i][j] == MINE)
// jb[i][j].setText(i+&,&+j);
// listener add
jb[i][j].addActionListener(this);
jb[i][j].addMouseListener(this);
jb[i][j].setName(i + &_& + j); // 方便点击是判断是点击了哪个按钮
// Font font = new Font(Font.SERIF, Font.BOLD, 10);
// jb[i][j].setFont(font);
// jb[i][j].setText(i+&,&+j);
jb[i][j].setBounds(j * BUTTON_BORDER + START_X, i
* BUTTON_BORDER + START_Y, BUTTON_BORDER, BUTTON_BORDER);
this.add(jb[i][j]);
public void init() {
jl.setText(&欢迎测试,一共有& + MINE_COUNT + &个雷&);
jl.setVisible(true);
jl.setBounds(20, 20, 500, 30);
this.add(jl);
showTime.setText(&已用时:0 秒&);
showTime.setBounds(400, 20, 100, 30);
this.add(showTime);
makeMine();
makeButton();
this.setSize(550, 600);
this.setLocation(700, 100);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
public Test(String title) {
super(title);
this.setLayout(null); //不使用布局管理器,每个控件的位置用setBounds设定
jb = new JButton[MINE_SIZE][MINE_SIZE];
jl = new JLabel();
showTime = new JLabel();
map = new int[MINE_SIZE][MINE_SIZE]; // 将按钮映射到数组中
public static void main(String[] args) {
Test test = new Test(&Hello Miner!&);
test.init();
test.run();
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if ((obj instanceof JButton) == false) {
showMessage(&错误&, &内部错误&);
String[] tmp_str = ((JButton) obj).getName().split(&_&);
x = Integer.parseInt(tmp_str[0]);
y = Integer.parseInt(tmp_str[1]);
if (map[x][y] == MINE) {
showMessage(&死亡&, &你踩到地雷啦~~~&);
showMine();
dfs(x, y, 0);
checkSuccess();
* 每次点击完后,判断有没有把全部雷都找到 通过计算状态为enable的按钮的个数来判断
private void checkSuccess() {
int cnt = 0;
for (int i = 0; i & MINE_SIZE; i++) {
for (int j = 0; j & MINE_SIZE; j++) {
if (jb[i][j].isEnabled()) {
if (cnt == MINE_COUNT) {
String tmp_str = showTime.getText();
tmp_str = tmp_str.replaceAll(&[^0-9]&, &&);
showMessage(&胜利&, &本次扫雷共用时:& + tmp_str + &秒&);
showMine();
private int dfs(int x, int y, int d) {
map[x][y] = CHECKED;
int i, tx, ty, cnt = 0;
for (i = 0; i & 8; i++) {
tx = x + mv[i][0];
ty = y + mv[i][1];
if (tx &= 0 && tx & MINE_SIZE && ty &= 0 && ty & MINE_SIZE) {
if (map[tx][ty] == MINE) {
cnt++;// 该点附近雷数统计
} else if (map[tx][ty] == EMPTY) {
} else if (map[tx][ty] == CHECKED) {
if (cnt == 0) {
for (i = 0; i & 8; i++) {
tx = x + mv[i][0];
ty = y + mv[i][1];
if (tx &= 0 && tx & MINE_SIZE && ty &= 0 && ty & MINE_SIZE
&& map[tx][ty] != CHECKED) {
dfs(tx, ty, d + 1);
jb[x][y].setText(cnt + &&);
jb[x][y].setEnabled(false);
* 在jl标签上显示一些信息
* @param title
* @param info
private void showMessage(String title, String info) {
jl.setText(info);
System.out.println(&in functino showMessage()
& + info);
public void run() {
int t = 0;
while (true) {
if (flag) {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
showTime.setText(&已用时:& + t + & 秒&);
// showMine();
private void showMine() {
Icon iconMine = new ImageIcon(&e:/mine.jpg&);
for (int i = 0; i & MINE_SIZE; i++) {
for (int j = 0; j & MINE_SIZE; j++) {
if (map[i][j] == MINE) {
jb[i][j].setText(&#&);
jb[i][j].setIcon(iconMine);
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 3) {
Object obj = e.getSource();
if ((obj instanceof JButton) == false) {
showMessage(&错误&, &内部错误&);
String[] tmp_str = ((JButton) obj).getName().split(&_&);
int x = Integer.parseInt(tmp_str[0]);
int y = Integer.parseInt(tmp_str[1]);
if (&{1}.equals(jb[x][y].getText())) {
jb[x][y].setText(&&);
jb[x][y].setText(&{1});
if(jb[x][y].getIcon() == null){
jb[x][y].setIcon(new ImageIcon(&e:/flag.jpg&));
jb[x][y].setIcon(null);
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:104077次
积分:1381
积分:1381
排名:千里之外
原创:40篇
评论:18条
(1)(3)(9)(6)(5)(2)(2)(2)(1)(10)>> 一个简单的Java扫雷小程序
一个简单的Java扫雷小程序
所属分类:
下载地址:
test4.zip文件大小:12.43 kB
分享有礼! 》
请点击右侧的分享按钮,把本代码分享到各社交媒体。
通过您的分享链接访问Codeforge,每来2个新的IP,您将获得0.1 积分的奖励。
通过您的分享链接,每成功注册一个用户,该用户在Codeforge上所获得的每1个积分,您都将获得0.2 积分的分成奖励。
这是一个简单的Java扫雷小程序,需要使用eclipse软件运行该程序,程序可以实现设置地雷数目和开始游戏的功能,界面为固定的12行*12列。主要能实现以下功能1.点击鼠标左键可寻找地雷;2. 鼠标右键标记地雷(F标记此处为地雷,Q标记此处是否有雷仍存在疑问);3.左键点击“开始”按钮,可选择开始游戏或者重新开始游戏;4.文本框可输入设置地雷数目(5-49之间)
Sponsored links
源码文件列表
温馨提示: 点击源码文件名可预览文件内容哦 ^_^
.classpath622.00 B 15:42
.project381.00 B 15:32
org.eclipse.jdt.core.prefs603.00 B 15:32
Bomb.class563.00 B 17:23
Bomb_actionAdapter.class598.00 B 17:23
Bomb_mouseAdapter.class565.00 B 17:23
Frame.class7.07 kB 17:23
Frame1_start_actionAdapter.class634.00 B 17:23
Main.class637.00 B 17:23
9.39 kB 17:02
1.55 kB 17:02
(提交有效评论获得积分)
评论内容不能少于15个字,不要超出160个字。
评价成功,多谢!
下载test4.zip
CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了
您的积分不足,优惠套餐快速获取 30 积分
10积分 / ¥100
30积分 / ¥200原价 ¥300 元
100积分 / ¥500原价 ¥1000 元
订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。
支付宝支付宝付款
微信钱包微信付款
更多付款方式:、
您本次下载所消耗的积分将转交上传作者。
同一源码,30天内重复下载,只扣除一次积分。
鲁ICP备号-3 runtime:Elapsed:231.292ms - init:0.1;find:0.6;t:0.4;tags:0.3;related:109.8;comment:0.2; 5.8
登录 CodeForge
还没有CodeForge账号?
Switch to the English version?
^_^"呃 ...
Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧18233人阅读
java(17)
package com.test.
import java.awt.event.ActionE
import java.awt.event.ActionL
import java.awt.event.MouseE
import java.awt.event.MouseL
import javax.swing.JB
import javax.swing.JF
import javax.swing.JL
* 这个是一个简单的扫雷例子,刚接触swing编写的,适合新手练习
* 该程序使用setBounds(x,y,w,h)对控件布局
* 做法参考win xp自带的扫雷,当然还写功能没做出来,
* 另外做出来的功能有些还存在bug
* @author Ping_QC
public class Test extends JFrame implements ActionListener, Runnable,
MouseListener {
private static final long serialVersionUID = -5039613L;
private final int EMPTY
private final int MINE
private final int CHECKED
private final int MINE_COUNT
= 10; // 雷的个数
private final int BUTTON_BORDER = 50; // 每个点的尺寸
private final int MINE_SIZE
= 10; // 界面规格, 20x20
private final int START_X
= 20; // 起始位置x
private final int START_Y
= 50; // 起始位置y
private JButton[][]
private JL
private JLabel showT
private int[][]
* 检测某点周围是否有雷,周围点的坐标可由该数组计算得到
private int[][] mv = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 },
{ 1, -1 }, { 0, -1 }, { -1, -1 } };
* 随机产生设定个数的雷
public void makeMine() {
int i = 0, tx,
for (; i & MINE_COUNT;) {
tx = (int) (Math.random() * MINE_SIZE);
ty = (int) (Math.random() * MINE_SIZE);
if (map[tx][ty] == EMPTY) {
map[tx][ty] = MINE;
i++; // 不记重复产生的雷
* 将button数组放到frame上,与map[][]数组对应
public void makeButton() {
for (int i = 0; i & MINE_SIZE; i++) {
for (int j = 0; j & MINE_SIZE; j++) {
jb[i][j] = new JButton();
// if (map[i][j] == MINE)
// jb[i][j].setText(i+&,&+j);
// listener add
jb[i][j].addActionListener(this);
jb[i][j].addMouseListener(this);
jb[i][j].setName(i + &_& + j); // 方便点击是判断是点击了哪个按钮
// Font font = new Font(Font.SERIF, Font.BOLD, 10);
// jb[i][j].setFont(font);
// jb[i][j].setText(i+&,&+j);
jb[i][j].setBounds(j * BUTTON_BORDER + START_X, i
* BUTTON_BORDER + START_Y, BUTTON_BORDER, BUTTON_BORDER);
this.add(jb[i][j]);
public void init() {
jl.setText(&欢迎测试,一共有& + MINE_COUNT + &个雷&);
jl.setVisible(true);
jl.setBounds(20, 20, 500, 30);
this.add(jl);
showTime.setText(&已用时:0 秒&);
showTime.setBounds(400, 20, 100, 30);
this.add(showTime);
makeMine();
makeButton();
this.setSize(550, 600);
this.setLocation(700, 100);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
public Test(String title) {
super(title);
this.setLayout(null); //不使用布局管理器,每个控件的位置用setBounds设定
jb = new JButton[MINE_SIZE][MINE_SIZE];
jl = new JLabel();
showTime = new JLabel();
map = new int[MINE_SIZE][MINE_SIZE]; // 将按钮映射到数组中
public static void main(String[] args) {
Test test = new Test(&Hello Miner!&);
test.init();
test.run();
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if ((obj instanceof JButton) == false) {
showMessage(&错误&, &内部错误&);
String[] tmp_str = ((JButton) obj).getName().split(&_&);
x = Integer.parseInt(tmp_str[0]);
y = Integer.parseInt(tmp_str[1]);
if (map[x][y] == MINE) {
showMessage(&死亡&, &你踩到地雷啦~~~&);
showMine();
dfs(x, y, 0);
checkSuccess();
* 每次点击完后,判断有没有把全部雷都找到 通过计算状态为enable的按钮的个数来判断
private void checkSuccess() {
int cnt = 0;
for (int i = 0; i & MINE_SIZE; i++) {
for (int j = 0; j & MINE_SIZE; j++) {
if (jb[i][j].isEnabled()) {
if (cnt == MINE_COUNT) {
String tmp_str = showTime.getText();
tmp_str = tmp_str.replaceAll(&[^0-9]&, &&);
showMessage(&胜利&, &本次扫雷共用时:& + tmp_str + &秒&);
showMine();
private int dfs(int x, int y, int d) {
map[x][y] = CHECKED;
int i, tx, ty, cnt = 0;
for (i = 0; i & 8; i++) {
tx = x + mv[i][0];
ty = y + mv[i][1];
if (tx &= 0 && tx & MINE_SIZE && ty &= 0 && ty & MINE_SIZE) {
if (map[tx][ty] == MINE) {
cnt++;// 该点附近雷数统计
} else if (map[tx][ty] == EMPTY) {
} else if (map[tx][ty] == CHECKED) {
if (cnt == 0) {
for (i = 0; i & 8; i++) {
tx = x + mv[i][0];
ty = y + mv[i][1];
if (tx &= 0 && tx & MINE_SIZE && ty &= 0 && ty & MINE_SIZE
&& map[tx][ty] != CHECKED) {
dfs(tx, ty, d + 1);
jb[x][y].setText(cnt + &&);
jb[x][y].setEnabled(false);
* 在jl标签上显示一些信息
* @param title
* @param info
private void showMessage(String title, String info) {
jl.setText(info);
System.out.println(&in functino showMessage()
& + info);
public void run() {
int t = 0;
while (true) {
if (flag) {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
showTime.setText(&已用时:& + t + & 秒&);
// showMine();
private void showMine() {
Icon iconMine = new ImageIcon(&e:/mine.jpg&);
for (int i = 0; i & MINE_SIZE; i++) {
for (int j = 0; j & MINE_SIZE; j++) {
if (map[i][j] == MINE) {
jb[i][j].setText(&#&);
jb[i][j].setIcon(iconMine);
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 3) {
Object obj = e.getSource();
if ((obj instanceof JButton) == false) {
showMessage(&错误&, &内部错误&);
String[] tmp_str = ((JButton) obj).getName().split(&_&);
int x = Integer.parseInt(tmp_str[0]);
int y = Integer.parseInt(tmp_str[1]);
if (&{1}.equals(jb[x][y].getText())) {
jb[x][y].setText(&&);
jb[x][y].setText(&{1});
if(jb[x][y].getIcon() == null){
jb[x][y].setIcon(new ImageIcon(&e:/flag.jpg&));
jb[x][y].setIcon(null);
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:104079次
积分:1381
积分:1381
排名:千里之外
原创:40篇
评论:18条
(1)(3)(9)(6)(5)(2)(2)(2)(1)(10)我是黄程程,一个讲编程的女生~分享原创视频,谢谢大家支持^_^
视频地址复制
Flash地址复制
Html地址复制
离线看更方便
用或其他应用扫描二维码
合集地址:/video//
广播电视节目制作经营许可证:(沪)字第1248号
| 网络文化经营许可证:沪网文[6号 | 信息网络传播视听节目许可证:0910417 | 互联网ICP备案:沪ICP备号-3 沪ICP证:沪B2- | 违法不良信息举报邮箱: | 违法不良信息举报电话:转3}

我要回帖

更多关于 扫雷java代码 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信