Java Swing實(shí)現(xiàn)掃雷小游戲
swing設(shè)計(jì)掃雷心得,供大家參考,具體內(nèi)容如下
最近學(xué)習(xí)swing學(xué)習(xí)之余做了一個(gè)小游戲:掃雷
1.前期設(shè)計(jì)

2.實(shí)現(xiàn)
其實(shí)完成這個(gè)游戲的核心就在于對數(shù)組的操縱,下面貼主要代碼Main.java:
package first;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class Main extends JFrame implements ActionListener, MouseListener {
/**
* 義建
*/
private static final long serialVersionUID = 1L;
// 前期參數(shù)聲明
JMenuItem JmiNew, JmiSave, JmiOpen, JmiExit, Jmichuji, Jmizhongji, Jmigaoji, JmishowInFo, JmiZiding;
Toolkit toolKit = Toolkit.getDefaultToolkit(); // 獲取默認(rèn)工具包。
Clipboard clipboard = toolKit.getSystemClipboard();// 獲取系統(tǒng) Calibrate
// 的一個(gè)實(shí)例,作為本機(jī)平臺提供的剪貼板工具的接口。
//兩個(gè)圖標(biāo)
ImageIcon icon = new ImageIcon("G:\\eclipse-workspace\\classTest_ThunderGame\\mine.png");
ImageIcon icon1 = new ImageIcon("G:\\eclipse-workspace\\classTest_ThunderGame\\flag.png");
private static int NUM = 1;// 這個(gè)NUM是雷數(shù),可以編寫一個(gè)程序來改變
// private static final int SNUM = 9;// 這個(gè)SNUM是掃雷的格數(shù),可以編寫一個(gè)程序來改變
private JButton[][] jb;
private int[][] map;
boolean[][] flags;
boolean[][] flag;
int coutTime;
// 聲明connection對象
Connection con;
// 驅(qū)動(dòng)程序名
String driver = "com.mysql.jdbc.Driver";
// url:指向要訪問的數(shù)據(jù)庫名
String url = "jdbc:mysql://localhost:3306/testsql3";
// mysql配置的用戶名
String user = "root";
// 密碼
String password = "huang";
public Main(int SNUM, int Mines) {// 主要界面構(gòu)造函數(shù)
setTitle("掃雷");
// 初始雷數(shù)量
NUM = Mines;
JMenuBar greenBar = new JMenuBar();// 菜單容器
greenBar.setOpaque(true);
greenBar.setBackground(new Color(250, 250, 250));
greenBar.setPreferredSize(new Dimension(800, 28));
greenBar.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
// 菜單
JMenu fileMenu1 = new JMenu("游戲");
JMenu fileMenu2 = new JMenu("難度");
JMenu fileMenu3 = new JMenu("幫助:");
greenBar.add(fileMenu1);
greenBar.add(fileMenu2);
greenBar.add(JmishowInFo = fileMenu3);
fileMenu1.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
fileMenu2.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
fileMenu3.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
// 菜單項(xiàng)
fileMenu1.add(JmiNew = new JMenuItem(" 新游戲 "));
fileMenu1.add(JmiSave = new JMenuItem(" 排行版 "));
fileMenu1.add(JmiZiding = new JMenuItem(" 自定義 "));
fileMenu1.addSeparator();
fileMenu1.add(JmiExit = new JMenuItem(" 退出 "));
fileMenu2.add(Jmichuji = new JMenuItem(" 初級 "));
fileMenu2.add(Jmizhongji = new JMenuItem(" 中級 "));
fileMenu2.add(Jmigaoji = new JMenuItem(" 高級 "));
fileMenu3.add(JmishowInFo = new JMenuItem(" 開發(fā)者信息 "));
JmiNew.addActionListener(this);
JmiExit.addActionListener(this);
JmiSave.addActionListener(this);
JmishowInFo.addActionListener(this);
Jmichuji.addActionListener(this);
Jmizhongji.addActionListener(this);
Jmigaoji.addActionListener(this);
JmiZiding.addActionListener(this);
JmiZiding.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
JmishowInFo.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
JmiNew.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
JmiSave.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
JmiExit.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
Jmichuji.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
Jmizhongji.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
Jmigaoji.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
setJMenuBar(greenBar);
Image icon = Toolkit.getDefaultToolkit().getImage("G:\\eclipse-workspace\\classTest_ThunderGame\\mine.png");
setIconImage(icon);
setLayout(new GridLayout(SNUM, SNUM));
jb = new JButton[SNUM][SNUM];
map = new int[SNUM][SNUM]; // 將按鈕映射到數(shù)組中
flags = new boolean[map.length][map[0].length];// 保存點(diǎn)開記錄表
flag = new boolean[map.length][map[0].length];// 保存點(diǎn)開記錄表
int count = 0;
// 布雷
while (count < NUM) {
int i = (int) (Math.random() * map.length);// hang
int j = (int) (Math.random() * map[0].length);// lie
if (map[i][j] != '*') {
map[i][j] = '*';
count++;
}
}
for (int i = 0; i < SNUM; i++) {
for (int j = 0; j < SNUM; j++) {
jb[i][j] = new JButton();
jb[i][j].setName(i + "_" + j);
jb[i][j].setBackground(new Color(220, 220, 220));
jb[i][j].setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 10));
jb[i][j].addActionListener(this);
jb[i][j].addMouseListener(this);// 加入mouse監(jiān)聽
add(jb[i][j]);
}
}
// 計(jì)時(shí)器
JLabel ststus = new JLabel();
JLabel Times = new JLabel();
JLabel miao = new JLabel();
add(ststus);
add(Times);
Times.setText(" 0 ");
miao.setText(" 秒");
setTimer(Times);
coutTime = 0;
ststus.setText(" 時(shí)間:");
greenBar.add(ststus);
greenBar.add(Times, RIGHT_ALIGNMENT);
greenBar.add(miao, RIGHT_ALIGNMENT);
Times.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
ststus.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
miao.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 16));
setSize(700, 700);
setLocationRelativeTo(null);
setVisible(true);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(DISPOSE_ON_CLOSE); // 加入這一行
}
private void setTimer(JLabel time) {// 時(shí)間監(jiān)聽
final JLabel varTime = time;
Timer timeAction = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
coutTime++;
varTime.setText("" + coutTime);
}
});
timeAction.start();
}
private void showTheClick(int x, int y) {// 點(diǎn)擊事件實(shí)現(xiàn)
if (map[x][y] == '*') {
jb[x][y].setIcon(icon);
showMines();
} else {
int count1 = 0;
for (int a = x - 1; a <= x + 1; a++) {
for (int b = y - 1; b <= y + 1; b++) {
if (!(a < 0 || b < 0 || b >= map[0].length || a >= map.length) && map[a][b] == '*')
count1++;
}
}
flags[x][y] = true;
if (count1 == 0) {
jb[x][y].setBackground(Color.white);
} else {
jb[x][y].setText(count1 + "");
jb[x][y].setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 20));
jb[x][y].setBackground(Color.white);
}
if (count1 == 0) {
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (!(i < 0 || j < 0 || i >= map.length || j >= map[0].length)) {
if (!(i == x && j == y) && flags[i][j] == false) {
showTheClick(i, j);//循環(huán)遍歷
} else {
// 防止重復(fù)訪問
}
}
}
}
}
}
}
private void showMines() {// 顯示所有雷
// TODO Auto-generated method stub
for (int i = 0; i < map.length; i++) {// 顯雷
for (int j = 0; j < map.length; j++) {
if (map[i][j] == '*') {
jb[i][j].setIcon(icon);
//
}
}
}
// 結(jié)束游戲
int b = JOptionPane.showOptionDialog(null, "哎呀,炸了炸了,新游戲?", "確認(rèn)框", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (b == 1) {
System.exit(0);
} else {
setVisible(false);
new Main(map.length,NUM);
}
}
@Override
public void actionPerformed(ActionEvent e) {// 事件監(jiān)聽處理
// TODO Auto-generated method stub
if (e.getSource() == JmiNew) {
setVisible(false);
new Main(map.length,NUM);
} else if (e.getSource() == JmiSave) {
showRange();
} else if (e.getSource() == JmiExit) {
System.exit(0);
} else if (e.getSource() == JmiZiding) {
new SelfMines();
} else if (e.getSource() == Jmichuji) {
setVisible(false);
new Main(5,3);
} else if (e.getSource() == JmishowInFo) {
new MyInfo();
} else if (e.getSource() == Jmizhongji) {
setVisible(false);
new Main(10,10);
} else if (e.getSource() == Jmigaoji) {
setVisible(false);
new Main(20,60);
} else {
Object obj = e.getSource();
int x, y;
String[] strM = ((JButton) obj).getName().split("_");
x = Integer.parseInt(strM[0]);
y = Integer.parseInt(strM[1]);
showTheClick(x, y);
checkSuccess();// 檢查是否游戲結(jié)束
}
}
private void showRange() {// 顯示排行榜
new Shiyan13(map.length);
}
private void checkSuccess() {//判斷游戲是否結(jié)束
// TODO Auto-generated method stub
int count = map.length * map[0].length;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
if (flags[i][j] == true)
count--;
}
}
if (count == NUM) {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");//表唯一標(biāo)示uuid
// 鏈接數(shù)據(jù)庫,存儲時(shí)間數(shù)據(jù)
try {
Class.forName(driver);
con = (Connection) DriverManager.getConnection(url, user, password);
String sql;
if (!con.isClosed()) {
// ta.setText("");
System.out.println("連接數(shù)據(jù)庫成功");
// 創(chuàng)建對象
Statement statement = (Statement) con.createStatement();
//
if (map.length == 10) {
// //要執(zhí)行的sql語句
sql = "insert into middlerange(userId,userTime) values(\"" + uuid + "\"," + coutTime + ");";
statement.executeUpdate(sql);
con.close();
} else if (map.length == 5) {
sql = "insert into rang(userid,userTime) values(\"" + uuid + "\"," + coutTime + ");";
statement.executeUpdate(sql);
con.close();
} else if (map.length == 20) {
sql = "insert into toprange(userId,userTime) values(\"" + uuid + "\"," + coutTime + ");";
statement.executeUpdate(sql);
con.close();
}else{
}
}
} catch (ClassNotFoundException e) {
// 數(shù)據(jù)庫驅(qū)動(dòng)類異常處理
System.out.println("error");
e.printStackTrace();
} catch (SQLException e) {
// System.out.println(e);
System.err.println("找不到數(shù)據(jù)");
// int i=JOptionPane.showConfirmDialog(null, "你輸入的sql語句有誤",
// "找不到",JOptionPane.YES_NO_OPTION);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("數(shù)據(jù)庫獲取數(shù)據(jù)成功!");
}
int i = JOptionPane.showOptionDialog(null, "恭喜你過關(guān)了,是否繼續(xù)?", "確認(rèn)框", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
// ststus.setText("hello"+i);
if (i == 1) {
System.exit(0);
} else {
setVisible(false);
new Main(map.length,NUM);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int c = e.getButton();
if (c == MouseEvent.BUTTON3) {
Object obj1 = e.getSource();
int x, y;
String[] strM = ((JButton) obj1).getName().split("_");
x = Integer.parseInt(strM[0]);
y = Integer.parseInt(strM[1]);
if (flag[x][y] == false && flags[x][y] == false) {//插旗子
jb[x][y].setIcon(icon1);
flag[x][y] = true;
} else {
jb[x][y].setIcon(null);
flag[x][y] = false;
}
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
3.實(shí)現(xiàn)效果

4.主要功能實(shí)現(xiàn)
(1)基礎(chǔ)掃雷功能(隨機(jī)布雷,插旗)
(2)可以選擇難度
(3)可以自定義掃雷的雷的數(shù)量以及格子數(shù)
(4)設(shè)置時(shí)間
(5)添加排行榜功能(根據(jù)時(shí)間存入數(shù)據(jù)庫排序)
(6)外打包成exe文件(帶jre)可以多平臺運(yùn)行. (使用exe4j打包jar包)
需要源碼的可以留個(gè)郵箱!這是源碼:掃雷游戲
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)按照大小寫字母順序排序的方法
這篇文章主要介紹了Java實(shí)現(xiàn)按照大小寫字母順序排序的方法,涉及java數(shù)組遍歷、編碼轉(zhuǎn)換、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例代碼
Tess4J是一個(gè)基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用,本文給大家介紹了SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例,需要的朋友可以參考下2024-12-12
SpringMvc導(dǎo)出Excel實(shí)例代碼
本篇文章主要介紹了SpringMvc導(dǎo)出Excel實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
使用SpringBoot讀取Windows共享文件的代碼示例
在現(xiàn)代企業(yè)環(huán)境中,文件共享是一個(gè)常見的需求,Windows共享文件夾(SMB/CIFS協(xié)議)因其易用性和廣泛的兼容性,成為了許多企業(yè)的首選,在Java應(yīng)用中,尤其是使用Spring Boot框架時(shí),如何讀取Windows共享文件是一個(gè)值得探討的話題,本文介紹了使用SpringBoot讀取Windows共享文件2024-11-11
Java如何實(shí)現(xiàn)壓縮文件與解壓縮zip文件
這篇文章主要介紹了Java如何實(shí)現(xiàn)壓縮文件與解壓縮zip文件問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

