扫雷java源代码

来源:原创作者:编辑:admin日期:2020-02-10 21:33

  import java.awt.*;

  import java.awt.event.*;

  import javax.swing.*;

  public class Frame

  extends JFrame {

  JTextField text;

  JLabel nowBomb, setBomb;

  int BombNum, BlockNum; // 以后雷数,以后方块数

  int rightBomb, restBomb, restBlock; // 找到的地雷数,残剩雷数,残剩方块数

  JButton start=new JButton(" 末尾 ");

  JPanel MenuPamel=new JPanel();

  JPanel bombPanel=new JPanel();

  Bomb[][] bombButton;

  JPanel c;

  BorderLayout borderLayout1=new BorderLayout();

  GridLayout gridLayout1=new GridLayout();

  public Frame() {

  try {

  setDefaultCloseOperation(EXIT_ON_CLOSE);

  jbInit();

  }

  catch (Exception exception) {

  exception.printStackTrace();

  }

  }

  private void jbInit() throws Exception {

  c=(JPanel) getContentPane();

  setTitle("365bet");

  c.setBackground(Color.WHITE);

  MenuPamel.setBackground(Color.GRAY);

  c.setLayout(borderLayout1);

  setSize(new Dimension(600, 600));

  setResizable(false);

  BlockNum=144;

  BombNum=10;

  text=new JTextField("10 ", 3);

  nowBomb=new JLabel("以后雷数" + ":" + BombNum);

  setBomb=new JLabel("设置地雷数");

  start.addActionListener(new Frame1_start_actionAdapter(this));

  MenuPamel.add(setBomb);

  MenuPamel.add(text);

  MenuPamel.add(start);

  MenuPamel.add(nowBomb);

  c.add(MenuPamel, java.awt.BorderLayout.SOUTH);

  bombPanel.setLayout(gridLayout1);

  gridLayout1.setColumns( (int) Math.sqrt(BlockNum));

  gridLayout1.setRows( (int) Math.sqrt(BlockNum));

  bombButton=new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];

  for (int i=0; i < (int) Math.sqrt(BlockNum); i++) {

  for (int j=0; j < (int) Math.sqrt(BlockNum); j++) {

  bombButton[i][j]=new Bomb(i, j);

  //bombButton[i][j].setSize(10, 10);

  bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大年夜小

  bombButton[i][j].setForeground(Color.white);

  bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));

  bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));

  bombPanel.add(bombButton[i][j]);

  }

  }

  c.add(bombPanel, java.awt.BorderLayout.CENTER);

  startBomb();

  }

  public void start_actionPerformed(ActionEvent e) {

  int num=Integer.parseInt(text.getText().trim());