编的一个小游戏,大概意思是有9个方块,随机设一块为地雷。点击不是地雷的方块变白色,点到地雷变黑色。踩到雷或者全部踩完不是雷的方格会弹出一个哭脸或笑脸框,3秒之后弹出对话框问是否继续游戏。我下面的代码可以运行了,但是不知道我不知道应该怎么把哭脸或笑脸框给关掉。重新游戏后原来的游戏框也不知道怎么关。。还有一个问题,不知道为什么有时候胜利后会连续弹出多个笑脸框!小弟是新手,在自学Java,请各位GGJJ多多帮忙!拜谢!!!
import java.awt.*;
import javax.swing.*;import java.awt.event.*;public class Minesweeper extends JFrame implements ActionListener {
int NUM = 9;
int random;
JButton[] button = new JButton[NUM];
JButton bomb = new JButton(); // 有雷的按钮
int total = 0;// 计算有几次没踩到雷 public Minesweeper() {
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);
//关掉当前的JFrame
} public void setBomb() {
int randomNum = (int) (Math.random() * 9);
bomb = button[randomNum];
random = randomNum;
System.out.println(random); // 随机生成雷 } public void actionPerformed(ActionEvent e) {
if (e.getSource() == bomb) {
bomb.setBackground(Color.black);
PaintFace frame = new PaintFace("lose");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,300);
frame.setVisible(true);
}
for (int j = 0; j < NUM; j++) {
if (e.getSource() == button[j] && j != random) {
if(button[j].getBackground().equals(Color.gray)){
total += 1;
button[j].setBackground(Color.white);
}
}
if (total == 8) {
PaintFace frame = new PaintFace("win"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,300);
frame.setVisible(true);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BeginDialog begin = new BeginDialog();
Minesweeper frame = new Minesweeper(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setVisible(true); }
}
public class PaintFace extends JFrame{
public PaintFace(String state){
if (state == "win"){
PaintHappy p1 = new PaintHappy();
getContentPane().add(p1);
p1.timer.start();
}
if (state == "lose"){
PaintSad p2 = new PaintSad();
getContentPane().add(p2);
p2.timer.start();
}
}
}class PaintHappy extends JPanel implements ActionListener{
boolean stop = false;
Timer timer = new Timer(3000, this);
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(200, 100, 30, 30);
g.setColor(Color.red);
g.fillOval(370, 100, 30, 30);
g.setColor(Color.red);
g.drawArc(200, 180, 200, 80, 0, -180);
}
public void actionPerformed(ActionEvent e){
timer.stop();
stop = true;
WinDialog win = new WinDialog();
if (win.ans == 1)
System.exit(0);
else {
JFrame frame1 = new Minesweeper();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(600, 300);
frame1.setVisible(true);// 重新游戏
}
}
}class PaintSad extends JPanel implements ActionListener{
boolean stop = false;
Timer timer = new Timer(3000, this);
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(200, 100, 30, 30);
g.setColor(Color.red);
g.fillOval(370, 100, 30, 30);
g.setColor(Color.red);
g.drawArc(200, 180, 200, 80, 0, 180);
} public void actionPerformed(ActionEvent e){ timer.stop();
LoseDialog lose = new LoseDialog();
if (lose.ans == 1)
System.exit(0);
else {
JFrame frame1 = new Minesweeper();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(600, 300);
frame1.setVisible(true);// 重新游戏
}
}
}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]);
}
}
import javax.swing.JOptionPane;
//开始游戏的对话框
public class BeginDialog {
BeginDialog(){
JOptionPane.showMessageDialog(null, "Welcome!", "Message", JOptionPane.INFORMATION_MESSAGE);
}
}
import java.awt.*;
import javax.swing.*;import java.awt.event.*;public class Minesweeper extends JFrame implements ActionListener {
int NUM = 9;
int random;
JButton[] button = new JButton[NUM];
JButton bomb = new JButton(); // 有雷的按钮
int total = 0;// 计算有几次没踩到雷 public Minesweeper() {
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);
//关掉当前的JFrame
} public void setBomb() {
int randomNum = (int) (Math.random() * 9);
bomb = button[randomNum];
random = randomNum;
System.out.println(random); // 随机生成雷 } public void actionPerformed(ActionEvent e) {
if (e.getSource() == bomb) {
bomb.setBackground(Color.black);
PaintFace frame = new PaintFace("lose");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,300);
frame.setVisible(true);
}
for (int j = 0; j < NUM; j++) {
if (e.getSource() == button[j] && j != random) {
if(button[j].getBackground().equals(Color.gray)){
total += 1;
button[j].setBackground(Color.white);
}
}
if (total == 8) {
PaintFace frame = new PaintFace("win"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,300);
frame.setVisible(true);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BeginDialog begin = new BeginDialog();
Minesweeper frame = new Minesweeper(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setVisible(true); }
}
public class PaintFace extends JFrame{
public PaintFace(String state){
if (state == "win"){
PaintHappy p1 = new PaintHappy();
getContentPane().add(p1);
p1.timer.start();
}
if (state == "lose"){
PaintSad p2 = new PaintSad();
getContentPane().add(p2);
p2.timer.start();
}
}
}class PaintHappy extends JPanel implements ActionListener{
boolean stop = false;
Timer timer = new Timer(3000, this);
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(200, 100, 30, 30);
g.setColor(Color.red);
g.fillOval(370, 100, 30, 30);
g.setColor(Color.red);
g.drawArc(200, 180, 200, 80, 0, -180);
}
public void actionPerformed(ActionEvent e){
timer.stop();
stop = true;
WinDialog win = new WinDialog();
if (win.ans == 1)
System.exit(0);
else {
JFrame frame1 = new Minesweeper();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(600, 300);
frame1.setVisible(true);// 重新游戏
}
}
}class PaintSad extends JPanel implements ActionListener{
boolean stop = false;
Timer timer = new Timer(3000, this);
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(200, 100, 30, 30);
g.setColor(Color.red);
g.fillOval(370, 100, 30, 30);
g.setColor(Color.red);
g.drawArc(200, 180, 200, 80, 0, 180);
} public void actionPerformed(ActionEvent e){ timer.stop();
LoseDialog lose = new LoseDialog();
if (lose.ans == 1)
System.exit(0);
else {
JFrame frame1 = new Minesweeper();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(600, 300);
frame1.setVisible(true);// 重新游戏
}
}
}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]);
}
}
import javax.swing.JOptionPane;
//开始游戏的对话框
public class BeginDialog {
BeginDialog(){
JOptionPane.showMessageDialog(null, "Welcome!", "Message", JOptionPane.INFORMATION_MESSAGE);
}
}
提个思路吧:
建议不要总是新建JFrame,改用替换JPanel,将新生成的Panel放到JFrame中,进行刷新
import javax.swing.*;import java.awt.event.*;public class Minesweeper extends JFrame implements ActionListener
{
int NUM = 9;
int random;
JButton[] button = new JButton[NUM];
JButton bomb = new JButton(); // 有雷的按钮
int total = 0;// 计算有几次没踩到雷 private static Minesweeper minesweeper; public static Minesweeper getMinesweeper()
{
if (minesweeper == null)
{
minesweeper = new Minesweeper();
}
else
{
for (int i = 0; i < minesweeper.NUM; i++)
{
minesweeper.button[i].setBackground(Color.gray);
}
minesweeper.setBomb(); // 设置雷
minesweeper.total=0;
}
return minesweeper;
} private Minesweeper()
{
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); // 关掉当前的JFrame
} public void setBomb()
{
int randomNum = (int) (Math.random() * 9);
bomb = button[randomNum];
random = randomNum;
System.out.println(random); // 随机生成雷 } public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bomb)
{
bomb.setBackground(Color.black);
PaintFace frame = new PaintFace("lose"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setVisible(true);
} for (int j = 0; j < NUM; j++)
{
if (e.getSource() == button[j] && j != random)
{
if (button[j].getBackground().equals(Color.gray))
{
total += 1;
button[j].setBackground(Color.white);
}
}
if (total == 8)
{
PaintFace frame = new PaintFace("win"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setVisible(true);
}
}
} public static void main(String[] args)
{
// TODO Auto-generated method stub
BeginDialog begin = new BeginDialog();
minesweeper = getMinesweeper();
// Minesweeper frame = new Minesweeper(); minesweeper.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
minesweeper.setSize(600, 300);
minesweeper.setVisible(true); }
}
{
public PaintFace(String state)
{
if (state == "win")
{
PaintHappy p1 = new PaintHappy();
getContentPane().add(p1);
p1.timer.start();
}
if (state == "lose")
{
PaintSad p2 = new PaintSad();
getContentPane().add(p2);
p2.timer.start(); }
}
}
class PaintHappy extends JPanel implements ActionListener
{
boolean stop = false;
Timer timer = new Timer(3000, this); public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(200, 100, 30, 30);
g.setColor(Color.red);
g.fillOval(370, 100, 30, 30);
g.setColor(Color.red);
g.drawArc(200, 180, 200, 80, 0, -180); } public void actionPerformed(ActionEvent e)
{
timer.stop();
stop = true;
WinDialog win = new WinDialog();
if (win.ans == 1)
System.exit(0);
else
{ JFrame frame1 = Minesweeper.getMinesweeper();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(600, 300);
frame1.setVisible(true);// 重新游戏
}
}
}
class PaintSad extends JPanel implements ActionListener
{
boolean stop = false;
Timer timer = new Timer(3000, this); public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(200, 100, 30, 30);
g.setColor(Color.red);
g.fillOval(370, 100, 30, 30);
g.setColor(Color.red);
g.drawArc(200, 180, 200, 80, 0, 180);
} public void actionPerformed(ActionEvent e)
{ timer.stop();
LoseDialog lose = new LoseDialog();
if (lose.ans == 1)
System.exit(0);
else
{ JFrame frame1 = Minesweeper.getMinesweeper();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(600, 300);
frame1.setVisible(true);// 重新游戏
}
}
}