作者微信 bishe2022

代码功能演示视频在页面下方,请先观看;如需定制开发,联系页面右侧客服
使用JAVA制作扫雷游戏

Custom Tab

package sailei;


import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.Insets;


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.JPanel;


public class MineSweepGame {

//声明需要的组件

private JFrame frame;

private Container contentPane;

private JPanel menuPanel,timePanel,gamePanel;

private JLabel timeLabel,resultLabel,mineCountLabel;

private JMenuItem menuItem1,menuItem2,menuItem3;

private JButton [][] buttons;

private int [] [] buttonsValue;

private int timeLength = 0;

private int row,col;

private int mineCount = 10;

//在构造方法中,创建组件,进行赋值

   public MineSweepGame() {

   row = 9;

   col = 9;

   

   frame = new JFrame ("中软卓越扫雷游戏。");

   contentPane = frame.getContentPane();

   

   menuPanel = new JPanel();

   timePanel = new JPanel();

   gamePanel = new JPanel();

   

   timeLabel = new JLabel("游戏时间:"+ new Integer(timeLength).toString()+"秒");

   resultLabel = new JLabel("状态:游戏进行中");

   mineCountLabel = new JLabel("剩余地雷数:"+mineCount);

   

   this.initButtonsAllValues();

   }

   //对按钮相关之进行赋值

   public void initButtonsAllValues(){

   buttons = new JButton[row + 2][col + 2];

   buttonsValue  = new int[row + 2][col+ 2];

   for (int i = 0;i < row + 2 ;i++){

   for (int j = 0;j < col + 2 ;j++){

   buttons[i][j] = new JButton();

   buttons[i][j].setMargin(new Insets ( 0, 0, 0, 0));

   buttons[i][j].setFont(new Font(null,Font.BOLD,25));

   buttons[i][j].setText("");

   buttonsValue[i][j] = 0;

   

   }

   }

   }

   

   //对组件进行布局

   public void initGame() {

   JMenuBar menuBar = new JMenuBar();

   JMenu menu = new JMenu("游戏设置");

   menuItem1 = new JMenuItem("初级");

   menuItem2 = new JMenuItem("中级");

   menuItem3 = new JMenuItem("再来一次");

   menuBar.add(menu);

   menu.add(menuItem1);

   menu.add(menuItem2);

   menu.add(menuItem3);

   menuPanel.add(menuBar);

   

   timePanel.add(menuItem1);

   timePanel.add(mineCountLabel);

   timePanel.add(resultLabel);

   

   gamePanel.setLayout(new GridLayout(row, col , 0 ,0));

   for(int i = 1; i<= row; i++) {

   for(int j = 1; j<= col; j++) {

   gamePanel.add(buttons[i][j]);

   }

   }

   JPanel panel = new JPanel();

   panel.setLayout(new BorderLayout());

   panel.add(menuPanel, BorderLayout.NORTH);

   panel.add(timePanel, BorderLayout.SOUTH);

   contentPane.add(panel, BorderLayout.NORTH);

   contentPane.add(gamePanel, BorderLayout.CENTER);

   

   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   frame.pack();

   frame.setSize(297, 377);

   frame.setBounds(400, 100 ,400 ,500);

   frame.setVisible(true);

   

   //设置地雷

   setMines(mineCount);

   }

   //设置地雷

   public void setMines(int mineCount) {

   this.mineCount = mineCount;

   int[] randomValue = new int [mineCount];

   //minneCount是地雷的个数。先获得10个不重复的随机数。然后通过随机数计算出雷的位置,保证随机数

   for(int i = 0; i< mineCount; i++){

   int temp = (int)(Math.random() * row * col);

   for(int j = 0; j< randomValue.length; j++ ){

   if (randomValue[j] == temp){

   temp = (int) (Math.random() * row * col);

   j++;

   }

   }

   randomValue[i] = temp;

   //把随机数转换成坐标

   int x =randomValue[i] / col +1;

   int y = randomValue[i] / col +1;

   //对应坐标的位置设置地雷

   buttonsValue[x][y]=10;

   //临时显示地雷位置,作为测试使用。每次运行,随机产生。

   buttons[x][y].setText("Q");

   

   }

   }

}


Home