最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Swing實現(xiàn)掃雷小游戲

 更新時間:2018年07月02日 14:59:45   作者:TOM_YIJIAN  
這篇文章主要為大家詳細介紹了Java Swing實現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

swing設(shè)計掃雷心得,供大家參考,具體內(nèi)容如下

最近學(xué)習(xí)swing學(xué)習(xí)之余做了一個小游戲:掃雷

1.前期設(shè)計

2.實現(xiàn)

其實完成這個游戲的核心就在于對數(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(); // 獲取默認工具包。

 Clipboard clipboard = toolKit.getSystemClipboard();// 獲取系統(tǒng) Calibrate
       // 的一個實例,作為本機平臺提供的剪貼板工具的接口。
 //兩個圖標
 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;// 這個NUM是雷數(shù),可以編寫一個程序來改變
 // private static final int SNUM = 9;// 這個SNUM是掃雷的格數(shù),可以編寫一個程序來改變
 private JButton[][] jb;
 private int[][] map;
 boolean[][] flags;
 boolean[][] flag;
 int coutTime;

 // 聲明connection對象
 Connection con;
 // 驅(qū)動程序名
 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));
 // 菜單項
 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];// 保存點開記錄表
 flag = new boolean[map.length][map[0].length];// 保存點開記錄表
 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]);

  }

 }

 // 計時器
 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("      時間:");
 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) {// 時間監(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) {// 點擊事件實現(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, "哎呀,炸了炸了,新游戲?", "確認框", 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("-", "");//表唯一標示uuid

  // 鏈接數(shù)據(jù)庫,存儲時間數(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ū)動類異常處理
  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ù)?", "確認框", 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.實現(xiàn)效果

4.主要功能實現(xiàn)

(1)基礎(chǔ)掃雷功能(隨機布雷,插旗)
(2)可以選擇難度
(3)可以自定義掃雷的雷的數(shù)量以及格子數(shù)
(4)設(shè)置時間
(5)添加排行榜功能(根據(jù)時間存入數(shù)據(jù)庫排序)
(6)外打包成exe文件(帶jre)可以多平臺運行. (使用exe4j打包jar包)

需要源碼的可以留個郵箱!這是源碼:掃雷游戲

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • RocketMQ消息中間件超詳細解讀

    RocketMQ消息中間件超詳細解讀

    這篇文章主要介紹了RocketMQ消息中間件超詳細解讀,RocketMQ作為一款純java、分布式、隊列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時消息、消息回溯等,本文就來詳細解讀一下,需要的朋友可以參考下
    2023-05-05
  • Java正則表達式循環(huán)匹配字符串方式

    Java正則表達式循環(huán)匹配字符串方式

    這篇文章主要介紹了Java正則表達式循環(huán)匹配字符串方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java中如何避免sql注入實例詳解

    Java中如何避免sql注入實例詳解

    SQL注入是最常見的攻擊方式之一,它不是利用操作系統(tǒng)或其它系統(tǒng)的漏洞來實現(xiàn)攻擊的,而是程序員因為沒有做好判斷,被不法用戶鉆了SQL的空子,下面這篇文章主要給大家介紹了關(guān)于Java中如何避免sql注入的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Java實現(xiàn)按照大小寫字母順序排序的方法

    Java實現(xiàn)按照大小寫字母順序排序的方法

    這篇文章主要介紹了Java實現(xiàn)按照大小寫字母順序排序的方法,涉及java數(shù)組遍歷、編碼轉(zhuǎn)換、判斷等相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • SpringBoot集成Tess4J實現(xiàn)OCR的示例代碼

    SpringBoot集成Tess4J實現(xiàn)OCR的示例代碼

    Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用,本文給大家介紹了SpringBoot集成Tess4J實現(xiàn)OCR的示例,需要的朋友可以參考下
    2024-12-12
  • SpringMvc導(dǎo)出Excel實例代碼

    SpringMvc導(dǎo)出Excel實例代碼

    本篇文章主要介紹了SpringMvc導(dǎo)出Excel實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Java中使用注解的實例詳解

    Java中使用注解的實例詳解

    注解(Annotation)是放在Java源碼的類、方法、字段、參數(shù)前的一種特殊“注釋”,這篇文章主要介紹了Java中如何使用注解,需要的朋友可以參考下
    2023-06-06
  • 使用SpringBoot讀取Windows共享文件的代碼示例

    使用SpringBoot讀取Windows共享文件的代碼示例

    在現(xiàn)代企業(yè)環(huán)境中,文件共享是一個常見的需求,Windows共享文件夾(SMB/CIFS協(xié)議)因其易用性和廣泛的兼容性,成為了許多企業(yè)的首選,在Java應(yīng)用中,尤其是使用Spring Boot框架時,如何讀取Windows共享文件是一個值得探討的話題,本文介紹了使用SpringBoot讀取Windows共享文件
    2024-11-11
  • Java如何實現(xiàn)壓縮文件與解壓縮zip文件

    Java如何實現(xiàn)壓縮文件與解壓縮zip文件

    這篇文章主要介紹了Java如何實現(xiàn)壓縮文件與解壓縮zip文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • MyBatis快速入門(簡明淺析易懂)

    MyBatis快速入門(簡明淺析易懂)

    MyBatis是支持普通SQL查詢,存儲過程和高級映射的優(yōu)秀持久層框架。mybatis的學(xué)習(xí)是程序員的必修課。今天小編通過分享本教程幫助大家快速入門mybatis,對mybatis入門知識感興趣的朋友參考下吧
    2016-11-11

最新評論

理塘县| 治多县| 绍兴县| 邵阳县| 高要市| 阳谷县| 黑龙江省| 怀化市| 郓城县| 井陉县| 乐业县| 秭归县| 唐山市| 山西省| 门头沟区| 稻城县| 叶城县| 泽库县| 洪雅县| 奉贤区| 金溪县| 开鲁县| 商洛市| 大荔县| 金溪县| 陕西省| 陕西省| 咸阳市| 安丘市| 台东市| 馆陶县| 秦皇岛市| 木里| 浪卡子县| 井陉县| 桐城市| 龙胜| 长泰县| 永修县| 合阳县| 全椒县|