⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form.java

📁 用java写的一个扫雷游戏。界面设计用jigloo完成。
💻 JAVA
字号:
package minesweep;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Form implements MouseListener, ActionListener{
	MenuBar mbar;
	Menu gamemenu;
	MenuItem low, mid, high;
	JFrame MineSweep;
	JPanel console, game;
	JButton begin, restart;
	JTextField minenum;
	block[][] bblock;
	Icon Bomb, Flag, Unsure, Clicked, Smile, Wrong, Explosion, Sad, Happy;
	Icon[] Item = new Icon[8];
	int width, heigh;
	int Numofmine, staticNum;
	int sel = 1;

	public Form() {
		/*初始化图标*/
		Bomb = new ImageIcon("images/mine.gif");
		Flag = new ImageIcon("images/flag.gif");
		Unsure = new ImageIcon("images/question.gif");
		Smile = new ImageIcon("images/smile.gif");
		Wrong = new ImageIcon("images/wrong.gif");
		Explosion = new ImageIcon("images/explosion.gif");
		Sad = new ImageIcon("images/sad.gif");
		Happy = new ImageIcon("images/happy.gif");
		Clicked = new ImageIcon("images/0.gif");
		Item[0] = new ImageIcon("images/1.gif");
		Item[1] = new ImageIcon("images/2.gif");
		Item[2] = new ImageIcon("images/3.gif");
		Item[3] = new ImageIcon("images/4.gif");
		Item[4] = new ImageIcon("images/5.gif");
		Item[5] = new ImageIcon("images/6.gif");
		Item[6] = new ImageIcon("images/7.gif");
		Item[7] = new ImageIcon("images/8.gif");
		//初始化界面
		MineSweep = new JFrame("扫雷");
		restart = new JButton();
		restart.addMouseListener(new restartListener());
		minenum = new JTextField(" " + Numofmine + " ");
		console = new JPanel();
		game = new JPanel();
		//构建扫雷界面
		refresh(sel);

	}

	public void refresh(int s) { // refresh the game
		// s=sel;
		sel = s;
		restart.setPreferredSize(new Dimension(20, 20));
		restart.setIcon(Smile);
		final JLabel text1 = new JLabel("雷数:");
		console.setLayout(new FlowLayout());
		console.add(restart);
		console.add(text1);
		console.add(minenum);
		menuCreate();
		MineSweep.add(BorderLayout.NORTH, console);
		MineSweep.add(BorderLayout.CENTER, game);
		MineSweep.setVisible(true);

		game.setVisible(true);
		switch (s) {
		case 1:				//初级
			width = 9;
			heigh = 9;
			Numofmine = 10;
			staticNum = Numofmine;
			minenum.setText(" " + Numofmine + " ");
			game.setLayout(new GridLayout(9, 9));
			bblock = new block[9][9];
			for (int i = 0; i < 9; i++) {
				for (int j = 0; j < 9; j++) {
					bblock[i][j] = new block(i, j);
					bblock[i][j].setPreferredSize(new Dimension(16, 16));
					game.add(bblock[i][j]);
				}
			}
			mineInit();
			console.setSize(16 * 9, 30);
			game.setSize(16 * 9, 16 * 9);
			MineSweep.pack();
			MineSweep.setResizable(false);
			break;
		case 2:				//中级
			width = 16;
			heigh = 16;
			Numofmine = 40;
			staticNum = Numofmine;
			minenum.setText(" " + Numofmine + " ");
			game.setLayout(new GridLayout(16, 16));
			bblock = new block[16][16];
			for (int i = 0; i < 16; i++) {
				for (int j = 0; j < 16; j++) {
					bblock[i][j] = new block(i, j);
					bblock[i][j].setPreferredSize(new Dimension(16, 16));
					game.add(bblock[i][j]);
				}
			}
			mineInit();
			console.setSize(16 * 16, 30);
			game.setSize(16 * 16, 16 * 16);
			MineSweep.pack();
			MineSweep.setResizable(false);
			break;
		case 3:				//高级
			width = 30;
			heigh = 16;
			Numofmine = 99;
			staticNum = Numofmine;
			minenum.setText(" " + Numofmine + " ");
			game.setLayout(new GridLayout(16, 30));
			bblock = new block[16][30];
			for (int i = 0; i < 16; i++) {
				for (int j = 0; j < 30; j++) {
					bblock[i][j] = new block(i, j);
					bblock[i][j].setPreferredSize(new Dimension(16, 16));
					game.add(bblock[i][j]);
				}
			}
			mineInit();
			console.setSize(16 * 30, 30);
			game.setSize(16 * 30, 16 * 16);
			MineSweep.pack();
			MineSweep.setResizable(false);
			break;
		}

		MineSweep.repaint();
	}

	public void mineInit() { // initial mines 布雷
		int x, y;
		int count = 0;
		while (count < staticNum) {
			x = (int) (Math.random() * heigh - 1);
			y = (int) (Math.random() * width);
			if (bblock[x][y].isBomb == 0) {
				bblock[x][y].isBomb = 1;
				// bblock[x][y].setText("0");
				count++;
			}
		}
		for (int i = 0; i < heigh; i++) {
			for (int j = 0; j < width; j++) {
				bblock[i][j].addMouseListener(this);// ??
				 bblock[i][j].addActionListener(this);
				checkMine(i, j);
			}
		}
	}

	public void checkMine(int a, int b) { // check the number of mines of the
										// around block
		int count = 0;
		for (int i = a - 1; i < a + 2; i++) {
			for (int j = b - 1; j < b + 2; j++) {
				if (i < 0 || i > heigh - 1 || j < 0 || j > width - 1)
					continue;
				count += bblock[i][j].isBomb;
			}
			bblock[a][b].BombsNumAround = count;
		}
	}

	public void clearBlock(int a, int b) { // clear if the around block has no
											// mine.
		if (bblock[a][b].BombsNumAround == 0) {
			for (int i = a - 1; i < a + 2; i++) {
				for (int j = b - 1; j < b + 2; j++) {
					if (i < 0 || i > heigh - 1 || j < 0 || j > width - 1)
						continue;
					else {
						if (bblock[i][j].BombFlag == 0
								&& bblock[i][j].isBomb == 0) {
							if (!bblock[i][j].isClicked) {
								bblock[i][j].isClicked = true;
								bblock[i][j].setEnabled(false);
								bblock[i][j].removeMouseListener(this);
								 bblock[i][j].removeActionListener(this);
								if (bblock[i][j].BombsNumAround == 0) {
									clearBlock(i, j);
								} else {
									bblock[i][j]
											.setIcon(Item[bblock[i][j].BombsNumAround - 1]);
									bblock[i][j].setEnabled(false);
								}
							}
						}
					}
				}
			}
		} else {
			bblock[a][b].isClicked = true;
			bblock[a][b].setIcon(Item[bblock[a][b].BombsNumAround - 1]);
			bblock[a][b].setEnabled(false);

		}
	}

	public void lose() {		//判断是否失败
		for (int i = 0; i < heigh; i++) {
			for (int j = 0; j < width; j++) {
				if (bblock[i][j].isBomb == 1 && bblock[i][j].isRight == false) {
					if (bblock[i][j].isClicked) {
						bblock[i][j].setIcon(Explosion);
					} else {
						bblock[i][j].setIcon(Wrong);
					}
					// bblock[i][j].setText("l");
				}
				if (bblock[i][j].isBomb == 1 && bblock[i][j].BombFlag == 1) {
					bblock[i][j].setIcon(Bomb);
				}
				bblock[i][j].removeMouseListener(this);
				 bblock[i][j].removeActionListener(this);
			}
		}
		restart.setIcon(Sad);
	}

	public void win() {		//判断是否成功
		int BombFlagNum = 0;
		int BlockClickedNum = 0;
		for (int i = 0; i < heigh; i++) {
			for (int j = 0; j < width; j++) {
				if (bblock[i][j].isBomb == 0 && bblock[i][j].isClicked == true) {
					BlockClickedNum++;
				}
				if (bblock[i][j].isBomb == 1 && bblock[i][j].BombFlag == 1) {
					BombFlagNum++;
				}
			}
			if (BombFlagNum == staticNum
					|| BlockClickedNum == width * heigh - staticNum) {
				for (i = 0; i < heigh; i++) {
					for (int j = 0; j < width; j++) {
						bblock[i][j].removeMouseListener(this);
						bblock[i][j].removeActionListener(this);
						if (bblock[i][j].isBomb == 1) {
							bblock[i][j].setIcon(Smile);
						}
					}
				}
				restart.setIcon(Happy);
			}
		}
	}

@Override
public void actionPerformed(ActionEvent e) {	//左键监听
	
	 if (((block) e.getSource()).isBomb == 0
	 && ((block) e.getSource()).isClicked == false
	&& ((block) e.getSource()).BombFlag == 0
	&& ((block) e.getSource()).isRight ==false) {
	int i = ((block) e.getSource()).x;
	 int j = ((block) e.getSource()).y;
	 clearBlock(i, j);
	win();
	} else {
	 if (((block) e.getSource()).isBomb == 1 && ((block) e.getSource()).isRight==false) {
	lose();
	}
	 }
	}

	@Override
	public void mouseClicked(MouseEvent e) {	//右键监听
		block blockclick = (block) e.getSource();

		if (e.getModifiers()==4) {
			if (Numofmine > 0 && blockclick.getIcon() == null) {
				blockclick.setIcon(Flag);
				blockclick.isRight = true;
				blockclick.BombFlag=1;
				Numofmine--;
				minenum.setText(" " + Numofmine + " ");
			} else {
				if (blockclick.getIcon() == Flag) {
						Numofmine++;
					blockclick.setIcon(Unsure);
					minenum.setText(" " + Numofmine + " ");
					blockclick.isRight = true;
					blockclick.BombFlag=0;
				} else {
					if (blockclick.getIcon() == Unsure) {
						blockclick.setIcon(null);
						blockclick.isRight = false;
						blockclick.BombFlag=0;
					}
				}
			}

			win();
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void menuCreate() {		//建立菜单栏
		mbar = new MenuBar();
		gamemenu = new Menu("游戏");

		low = new MenuItem("初级");
		mid = new MenuItem("中级");
		high = new MenuItem("高级");

		gamemenu.add(low);
		gamemenu.add(mid);
		gamemenu.add(high);
		mbar.add(gamemenu);

		MineSweep.setMenuBar(mbar);
		menuActionListener lowListener = new menuActionListener();
		menuActionListener midListener = new menuActionListener();
		menuActionListener highListener = new menuActionListener();
		lowListener.select = 1;
		midListener.select = 2;
		highListener.select = 3;
		low.addActionListener(lowListener);
		mid.addActionListener(midListener);
		high.addActionListener(highListener);
	}

	public class menuActionListener implements ActionListener {		//菜单栏动作监听服务类
		int select = 1;

		@Override
		public void actionPerformed(ActionEvent ae) {
			// MineSweep.removeAll();
			game.removeAll();
			console.removeAll();
			refresh(select);
			// TODO Auto-generated method stub

		}
	}

	public class restartListener implements MouseListener {			//重开一局按键监听服务类

		@Override
		public void mouseClicked(MouseEvent me) {
				game.removeAll();
				console.removeAll();
				refresh(sel);
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

	}

	public static void main(String[] args) {	//主函数 建立扫雷
		new Form();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -