62,614
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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);
}
}
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]);
}
}