import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class StartGame
{
private JFrame frame;
private JPanel panel;
private JButton[][] button = new JButton[9][9];
private int[][] status = new int[9][9];
public StartGame()
{
frame = new JFrame();
frame.setTitle("MineSweeper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
panel = new JPanel();
panel.setLayout(new GridLayout(9, 9));
for (int i = 0; i < button.length; i++)
{
for (int j = 0; j < button.length; j++)
{
button[i][j] = new JButton("");
panel.add(button[i][j]);
status[i][j] = 0;
button[i][j].addActionListener(new ButtonListener());
}
}
setMine();
frame.add(panel);
frame.setVisible(true);
}
public void setMine()
{
Random randomNumber = new Random();
for (int i = 0; i < 10; i++)
{
int x = randomNumber.nextInt(9);
int y = randomNumber.nextInt(9);
if (status[x][y] != 1)
{
status[x][y] = 1;
}
else
{
i--;
}
}
}
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for (int x = 0; x < button.length; x++)
{
for (int y = 0; y < button.length; y++)
{
if (e.getSource() == button[x][y])
{
if (status[x][y] == 0)
{
ButtonChange(x, y);
}
else
{
JOptionPane.showMessageDialog(null ,"YOU LOSS");
}
}
}
}
}
}
public void ButtonChange(int x, int y)
{
if (x >= 0 && y >= 0 && x < button.length && y < button.length)
{
int num = 0;
if (status[x][y] == 0)
{
num += CheckMine(x-1, y-1);
num += CheckMine(x, y-1);
num += CheckMine(x+1, y-1);
num += CheckMine(x-1, y);
num += CheckMine(x+1, y);
num += CheckMine(x-1, y+1);
num += CheckMine(x, y+1);
num += CheckMine(x+1, y+1);
}
if (num == 0)
{
button[x][y].setEnabled(false);
ButtonChange(x, y-1);
ButtonChange(x, y+1);
ButtonChange(x-1, y);
ButtonChange(x+1, y);
}
else
{
button[x][y].setEnabled(false);
button[x][y].setText(Integer.toString(num));
}
}
}
public int CheckMine(int x, int y)
{
int num = 0;
if (x >= 0 && y >= 0 && x < button.length && y < button.length)
{
if (status[x][y] == 1)
{
num = 1;
}
}
return num;
}
}
這是我的代碼,我還沒有還完,因爲我還沒有做檢查贏得了方法的錯誤。 主程序在另一個類中。當我嘗試點擊按鈕的一些我儘量讓實踐掃雷艇,但我得到了,我不明白爲什麼
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at javax.swing.JComponent.disable(JComponent.java:3638)
at java.awt.Component.enable(Component.java:1517)
at java.awt.Component.setEnabled(Component.java:1480)
at javax.swing.JComponent.setEnabled(JComponent.java:2680)
at javax.swing.AbstractButton.setEnabled(AbstractButton.java:2091)
at StartGame.ButtonChange(StartGame.java:104)
at StartGame.ButtonChange(StartGame.java:103)
: 我得到了一個錯誤。 我不明白爲什麼,因爲我檢查它,它似乎是正確的。 它沒有在ButtonChange函數中僅適用於上下按鈕更改,而不適用於左右。我只是沒有得到什麼發生在我的程序.....
這不是一個錯誤消息。請發佈完整的錯誤消息。 – Coderchu
請告訴我們錯誤信息(即上面的線'在StartGame.Button ...) – Roberto
關於「AWT-EventQueue的 - 0」 java.lang.StackOverflowError的 \t在javax.swing.JComponent中說 異常螺紋遺憾。禁用(JComponent.java:3638) \t在java.awt.Component.enable(Component.java:1517) \t在java.awt.Component.setEnabled(Component.java:1480) \t在javax.swing.JComponent中.setEnabled(JComponent.java:2680) \t at javax.swing.AbstractButton.setEnabled(AbstractButton。java:2091) –