import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class Minesweeper2 extends JFrame implements ActionListener {
int NUM = 9;
int random;
JButton[] button = new JButton[NUM];
JButton bomb = new JButton(); // 有雷的按钮
int total = 0; // 计算有几次没踩到雷 public Minesweeper2()
{
super("Bomb"); setLayout(new GridLayout(3, 3)); for (int i = 0; i < NUM; i++)
{
button[i] = new JButton();
button[i].setBackground(Color.gray);
getContentPane().add(button[i]);
} // 布局
setBomb(); // 设置雷 for (int i = 0; i < NUM; i++)
button[i].addActionListener(this); setSize(700, 300);
setVisible(true); } public void setBomb()
{
int randomNum = (int) (Math.random() * 8);
bomb = button[randomNum];
random = randomNum;
// System.out.println(random); // 随机生成雷 } public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bomb)
{
bomb.setBackground(Color.black);
LoseDialog lose = new LoseDialog();
if (lose.ans == 1)
System.exit(0);
else
{
for (int i = 0; i < NUM; i++)
{
button[i].setBackground(Color.gray);
}
setBomb();
total = 0; // 重新游戏
return;//这个return很重要
}
} for (int j = 0; j < NUM; j++)
{
if (e.getSource() == button[j] && j != random&&button[j].getBackground().equals(Color.gray))
{
button[j].setBackground(Color.white);
total += 1;
}
if (total == 8)
{
WinDialog win = new WinDialog();
if (win.ans == 1)
System.exit(0);
else
{
for (int i = 0; i < NUM; i++)
{
button[i].setBackground(Color.gray);
}
setBomb();
total = 0;// 重新游戏
return;//同上,return很重要
} }
}
} public static void main(String[] args)
{
// TODO Auto-generated method stub BeginDialog begin = new BeginDialog(); JFrame frame = new Minesweeper2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}if (e.getSource() == button[j] && j != random&&button[j].getBackground().equals(Color.gray))这里加了一个条件,因为之前发现如果一直点击同一个button够8次时虽然没有找到boom,但依然会提示赢得了游戏,是否要重玩。

解决方案 »

  1.   


    这个是什么东西呀?可以把这几个类也贴过来呀!LoseDialog
    WinDialog
    BeginDialog
      

  2.   

    不好意思,现在补全。
    import javax.swing.JOptionPane;//开始游戏的对话框 
    public class BeginDialog {
    BeginDialog()
    {
    JOptionPane.showMessageDialog(null, "Welcome!", "Message",
    JOptionPane.INFORMATION_MESSAGE);
    }
    }import javax.swing.JOptionPane;//失败后的对话框 
    public class LoseDialog {
    int ans; LoseDialog()
    {
    Object[] options = { "Yes", "No" };
    ans = JOptionPane.showOptionDialog(null,
    "BOOoom!!! Would you like to play again?", "",
    JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
    options, options[0]); }
    }import javax.swing.JOptionPane;//成功踩雷的对话框 
    public class WinDialog {
    int ans; WinDialog()
    {
    Object[] options = { "Yes", "No" };
    ans = JOptionPane.showOptionDialog(null,
    "Win!!! Would you like to play again?", "",
    JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
    options, options[0]); }}
      

  3.   

    其实楼主代码并没有什么问题,这个扫雷游戏就是让你不踩雷,所以摁了8次没踩到的话就算你赢
    这个代码前几天写过,再次贴上,:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;public class Minesweeper2 extends JFrame implements ActionListener {
    int NUM = 9; int random; JButton[] button = new JButton[NUM]; JButton bomb = new JButton(); // 有雷的按钮 int total = 0;// 计算有几次没踩到雷 ArrayList<Integer> label = new ArrayList<Integer>();// 放入已经踩过的方格编号 boolean neverRead = false; public Minesweeper2() {
    super("Bomb"); setLayout(new GridLayout(3, 3)); for (int i = 0; i < NUM; i++) {
    button[i] = new JButton();
    button[i].setBackground(Color.gray);
    getContentPane().add(button[i]);
    } // 布局
    setBomb(); // 设置雷 for (int i = 0; i < NUM; i++)
    button[i].addActionListener(this);
    } public void setBomb() {
    int randomNum = (int) (Math.random() * 8);
    bomb = button[randomNum];
    random = randomNum;
    System.out.println(" " + random); // 随机生成雷 } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == bomb) {
    bomb.setBackground(Color.black);
    LoseDialog lose = new LoseDialog();
    if (lose.ans == 1)
    System.exit(0);
    else {
    for (int i = 0; i < NUM; i++) {
    button[i].setBackground(Color.gray);
    }
    setBomb();
    total = 0; // 重新游戏
    neverRead = false;
    return;
    }
    } for (int j = 0; j < NUM; j++) {
    if (e.getSource() == button[j] && j != random) {
    if (total == 0) {
    label.add(j);
    button[j].setBackground(Color.white);
    total += 1;
    }
    if (total != 0) {
    for (int k = 0; k < total; k++) {
    if (j != label.get(k))
    neverRead = true;
    }
    }
    if (neverRead) {
    label.add(j);
    if (button[j].getBackground().equals(Color.gray))
    total += 1; // 这3个if是判断是否出现在同个方格上连续点击,不知道问题出现在哪
    button[j].setBackground(Color.white);
    System.out.println("  " + total);
    }
    }
    if (total == 8) {
    WinDialog win = new WinDialog();
    System.out.println("OK,Success!");
    if (win.ans == 1)
    System.exit(0);
    else {
    for (int i = 0; i < NUM; i++) {
    button[i].setBackground(Color.gray);
    }
    setBomb();
    total = 0;// 重新游戏
    neverRead = false;
    return;
    } }
    }
    } public static void main(String[] args) {
    // TODO Auto-generated method stub BeginDialog begin = new BeginDialog(); JFrame frame = new Minesweeper2();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700, 300);
    frame.setVisible(true); }
    }
      

  4.   

    呵呵,有参考价值,有空了我也写个跟系统自带的那个一样的,hoho