java版扫雷

这篇博客介绍了如何使用Java设计扫雷游戏,通过自定义MyJButton类,继承JButton并添加数字显示和背景图片功能。文章重点讲解了如何实现鼠标事件监听,包括左键点击时显示数字以及右键设置背景为雷的逻辑。此外,还提到了使用递归算法来显示数字,并在初始化阶段通过SetBound类静态方法随机生成炸弹位置。
摘要由CSDN通过智能技术生成
 //Gui类用JButton(MyJButton)数组存储全部按钮

//设计MyJButton类,继承JButton类,加上数字显示,背景图片,在鼠标响应时做出一系列的自变化

//界面初始化(刚刚进入游戏界面),实现对MyJButton类的背景数字或图片(地雷)的设定!

//对鼠标左右键的监听方法!(左键 调用MyJButton的init方法 (设置背景透明显示数字)  右键 设置背景雷)递归算法 显示数字!!

//创建Gui对象时首先用SetBound类的静态方法负责随机生成炸弹数列(炸弹按钮上的数字为0 默认!!) 然后随机用数列二层循环生成MyJButton类数组;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public class Gui extends JFrame implements ActionListener {
	static SouthPanel sp;
	static SetFrame sf;
	JMenuItem newgame;
	JMenuItem level;
	JMenuItem appearance;
	JMenuItem recordinfo;
	JMenuItem exit;
	JMenuItem about;
	JMenuItem method;
	MyJButton jb[][];
	public static HashSet hs;// 用于保存生成的随机炸弹数列
	public static boolean lasted = false; // 标志变量 标记是否重新开始新的游戏
	JPanel main;
	Gui() {
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setResizable(false);
		sp = new SouthPanel();
		MyMouseListenerAdapter.noBoundnum = 0; // 每次雷区初始化都重新设定noBoundnum的数值为0
												// (该变量用于判断扫雷是否成功)
		JMenuBar jmenubar = new JMenuBar();
		JMenu game = new JMenu("游戏");
		JMenu help = new JMenu("帮助");
		newgame = new JMenuItem("新游戏");

		newgame.addActionListener(this);

		game.add(new JSeparator());

		level = new JMenuItem("级别");

		level.addActionListener(this);

		recordinfo = new JMenuItem("统计信息");

		recordinfo.addActionListener(this);

		appearance = new JMenuItem("更改外观");

		appearance.addActionListener(this);

		exit = new JMenuItem("退出");

		exit.addActionListener(this);

		about = new JMenuItem("关于扫雷");

		about.addActionListener(this);

		method = new JMenuItem("玩法");

		method.addActionListener(this);

		game.add(newgame);
		game.add(level);
		game.add(recordinfo);
		game.add(appearance);
		game.add(exit);

		help.add(about);
		help.add(method);

		jmenubar.add(game);
		jmenubar.add(help);
		this.setJMenuBar(jmenubar);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jb = new MyJButton[sf.setheight + 1][sf.setwidth + 1];
		// 初始化小 默认为简单模式
		// ....................初始化炸弹...................................
		initBound(Gui.lasted);// 调用函数初始化炸弹!!
		main = new JPanel();
		main.setBackground(Color.CYAN);
		int num = 0;
		main.setLayout(new GridLayout(sf.setheight, sf.setwidth));
		for (int i = 1; i <= sf.setheight; i++) {
			for (int j = 1; j <= sf.setwidth; j++) {
				if (jb[i][j] == null) {
					jb[i][j] = new MyJButton(i, j);
					num++;
				}

				jb[i][j].addMouseListener(new MyMouseListenerAdapter(this));
				main.add(jb[i][j]); // 从第二个元素开始赋值!!
			}
		}
		for (int i = 1; i <= sf.setheight; i++) {
			for (int j = 1; j <= sf.setwidth; j++) {
				if (!jb[i][j].haveimage) {
					jb[i][j].num = seekAllBound(i, j);
				}
			}
		}

		this.add(main, BorderLayout.CENTER);
		sp.time.setText("" + SetBound.number * 6 + "");
		sp.bound.setText("" + SetBound.number + "");
		// 创建面板
		this.add(sp.jp, BorderLayout.SOUTH);
		this.setBounds(100, 100, 44 * sf.setwidth, 44 * sf.setheight + 30);
		this.setVisible(true);
		PlayAudio.playstart();
	}

	public void initBound(boolean lasted) {
		if (!lasted) {
			hs = SetBound.initBound(this.sf.setwidth, this.sf.setheight);
		}
		Iterator it = hs.iterator();
		int count = 1;
		while (it.hasNext()) {
			int location = (Integer) it.next();
			int hang;
			int lie;
			if (location % this.sf.setwidth != 0) {
				hang = location / this.sf.setwidth + 1;
				lie = location % this.sf.setwidth;

			} else {
				hang = location / this.sf.setwidth;
				lie = this.sf.setwidth;
			}
			this.jb[hang][lie] = new MyJButton(hang, lie);
			this.jb[hang][lie].haveimage = true;
			// 设置初始炸弹!!(给某些MJButton设置背景图片!!)

		}

	}

	public void select(int height, int width) {
		// 设置标志 MyMouseListenerAdapter的over为false!!
		MyMouseListenerAdapter.over = false;
		if (MyMouseListenerAdapter.timer != null) {
			MyMouseListenerAdapter.timer.cancel();
			MyMouseListenerAdapter.timer = null; // 取消正在进行的倒计时!
		}
		Random ran = new Random();
		this.getContentPane().removeAll(); // 清空上次的面板!!
		this.sf.setwidth = width;
		this.sf.setheight = height;
		jb = new MyJButton[height + 1][width + 1];
		initBound(lasted);// 调用函数初始化炸弹!!
		mai
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值