原创:享学课堂讲师
转载请声明出处!
前言
忘记是从何处看到过关于扫雷小程序的文章,所以这次也就跟着做一下。其实很简单的,如果有java入门的同学也可以尝试一下自己做这种java小程序。几行代码做几遍基本上能摸清楚这些基础了,对于编程能力也能提高一些。(虽然小编玩了半天也才仅仅赢了一把)
具体代码如下:
package saolei;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
public class saolei implements ActionListener {
JFrame frame=new JFrame("扫雷小游戏");
JButton reset=new JButton("重来");
Container container=new Container();
//游戏数据结构
final int row=20;
final int col=20;
final int leiCount=30;
JButton [][] buttons=new JButton[row][col];
int [][] counts=new int[row][col];
final int LEICODE=10;
// 构造函数
public saolei(){
//1、设置窗口
frame.setSize(1000, 800);
frame.setResizable(true);
//是否可改变窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//2、添加重来按钮
addResetButton();
//添加按钮
addButtons();
//埋雷
addLei();
//添加雷的计算
calcNeiboLei();
frame.setVisible(true);
}
public void addResetButton(){
reset.setBackground(Color.cyan);
reset.setOpaque(true);
reset.addActionListener(this