题目是要求做一个小游戏:有9个方格,随机设其中一个是地雷,点方格后若是地雷则方格变黑,若不是地雷则变白。我基本上已经写完了,可是不知道为什么我重新游戏后出了点小问题:踩中的地雷自动变成白色了代码红色部分
请大家多多帮忙啊!不甚感激啊!!第一次编游戏就碰到这种情况,太伤心了。。
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; //重新游戏
}
}
for (int j = 0; j < NUM; j++){
if (e.getSource() == button[j] && j != random){
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;//重新游戏
}
}
}
}
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);
}
import javax.swing.JOptionPane;
//开始游戏的对话框
public class BeginDialog {
BeginDialog(){
JOptionPane.showMessageDialog(null, "Welcome!", "Message", JOptionPane.INFORMATION_MESSAGE);
}
}
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]);
}
}
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 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; //重新游戏
}
}
for (int j = 0; j < NUM; j++){
if (e.getSource() == button[j] && j != random){
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;//重新游戏
}
}
}
}
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);
}
import javax.swing.JOptionPane;
//开始游戏的对话框
public class BeginDialog {
BeginDialog(){
JOptionPane.showMessageDialog(null, "Welcome!", "Message", JOptionPane.INFORMATION_MESSAGE);
}
}
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]);
}
}
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.*;
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;
}
} for (int j = 0; j < NUM; j++) {
if (e.getSource() == button[j] && j != random) {
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;
} }
}
} 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);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Minesweeper2 extends JFrame implements ActionListener{
int NUM = 9;
int random = (int) ( Math.random() * 8 );
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);
dispose(); //关掉原来的Frame,有错吗?
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700,300);
setVisible(true);
}
public void setBomb(){
bomb = button[random];
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{
new Minesweeper2(); //重新游戏
}
}
for (int j = 0; j < NUM; j++){
if (e.getSource() == button[j] && j != random){
button[j].setBackground(Color.white);
total += 1;
}
if (total == 8){
WinDialog win = new WinDialog();
if (win.ans == 1)
System.exit(0);
else{
new Minesweeper2(); //重新游戏
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new BeginDialog();
new Minesweeper2();
}
}