我制作了一个像游戏Minesweeper一样运行的程序。它被设置为GridLayout,主代码中有一个名为getNumberSmall的递归代码,在单击按钮时运行。它不会递归运行,除非点击的按钮是一个空格(即如果你曾经玩过扫雷,既不是我的也不是数字)。单击BLANK空间时,代码应该在单击的空白按钮周围的按钮上递归运行。 HOwever,当我这样做时,我得到一个堆栈溢出错误。我该如何解决这个问题?
public void getNumberSmall(JButton b3){
JButton above = new JButton();
JButton below = new JButton();
JButton right = new JButton();
JButton left = new JButton();
JButton DRight = new JButton();
JButton DLeft = new JButton();
JButton DRightD = new JButton();
JButton DLeftD = new JButton();
JButton clicked = b3;
int myX = (int) clicked.getClientProperty("column");
int myY = (int) clicked.getClientProperty("row");
int mNum = 0;
if(myX < 9){
for(int i = 0; i < small.size(); i++){
if((int) small.get(i).getClientProperty("column") - 1 == myX){
if((int) small.get(i).getClientProperty("row") == myY){
right = small.get(i);
if((int)right.getClientProperty("enabled") == 1){
right = null;
}
}
}
}
}
else{
right = null;
}
if(myX > 1){
for(int i = 0; i < small.size(); i++){
if((int) small.get(i).getClientProperty("column") + 1 == myX){
if((int) small.get(i).getClientProperty("row") == myY){
left = small.get(i);
if((int)left.getClientProperty("enabled") == 1){
left = null;
}
}
}
}
}
else{
left = null;
}
if(myY < 9){
for(int i = 0; i < small.size(); i++){
if((int) small.get(i).getClientProperty("column") == myX){
if((int) small.get(i).getClientProperty("row") - 1 == myY){
below = small.get(i);
if((int)below.getClientProperty("enabled") == 1){
below = null;
}
}
}
}
}
else{
below = null;
}
if(myY > 1){
for(int i = 0; i < small.size(); i++){
if((int) small.get(i).getClientProperty("column") == myX){
if((int) small.get(i).getClientProperty("row") + 1 == myY){
above = small.get(i);
if((int)above.getClientProperty("enabled") == 1){
above = null;
}
}
}
}
}
else{
above = null;
}
if(myY == 9){
DRightD = null;
DLeftD = null;
}
if(myY == 1){
DRight = null;
DLeft = null;
}
if(myX == 9){
DRightD = null;
DRight = null;
}
if(myX == 1){
DLeftD = null;
DLeft = null;
}
if(DRight != null){
for(int i = 0; i < small.size(); i++){
if((int) small.get(i).getClientProperty("column") - 1 == myX){
if((int) small.get(i).getClientProperty("row") + 1 == myY){
DRight = small.get(i);
if((int)DRight.getClientProperty("enabled") == 1){
DRight = null;
}
}
}
}
}
if(DLeft != null){
for(int i = 0; i < small.size(); i++){
if((int) small.get(i).getClientProperty("column") + 1 == myX){
if((int) small.get(i).getClientProperty("row") + 1 == myY){
DLeft = small.get(i);
if((int)DLeft.getClientProperty("enabled") == 1){
DLeft = null;
}
}
}
}
}
if(DRightD != null){
for(int i = 0; i < small.size(); i++){
if((int) small.get(i).getClientProperty("column") - 1 == myX){
if((int) small.get(i).getClientProperty("row") - 1 == myY){
DRightD = small.get(i);
if((int)DRightD.getClientProperty("enabled") == 1){
DRightD = null;
}
}
}
}
}
if(DLeftD != null){
for(int i = 0; i < small.size(); i++){
if((int) small.get(i).getClientProperty("column") + 1 == myX){
if((int) small.get(i).getClientProperty("row") - 1 == myY){
DLeftD = small.get(i);
if((int)DLeftD.getClientProperty("enabled") == 1){
DLeftD = null;
}
}
}
}
}
if(above != null){
if((int) above.getClientProperty("mines") == 1){
mNum = mNum + 1;
}
}
if(below != null){
if((int) below.getClientProperty("mines") == 1){
mNum = mNum + 1;
}
}
if(right != null){
if((int) right.getClientProperty("mines") == 1){
mNum = mNum + 1;
}
}
if(left != null){
if((int) left.getClientProperty("mines") == 1){
mNum = mNum + 1;
}
}
if(DRight != null){
if((int) DRight.getClientProperty("mines") == 1){
mNum = mNum + 1;
}
}
if(DLeft != null){
if((int) DLeft.getClientProperty("mines") == 1){
mNum = mNum + 1;
}
}
if(DRightD != null){
if((int) DRightD.getClientProperty("mines") == 1){
mNum = mNum + 1;
}
}
if(DLeftD != null){
if((int) DLeftD.getClientProperty("mines") == 1){
mNum = mNum + 1;
}
}
if(mNum == 0){
String num2 = "" + mNum;
clicked.setText(num2);
if((above != null) && ((int)above.getClientProperty("enabled") == 0)){
getNumberSmall(above);
above.putClientProperty("enabled", 1);
}
if((below != null) && ((int)below.getClientProperty("enabled") == 0)){
getNumberSmall(below);
below.putClientProperty("enabled", 1);
}
if((right != null) && ((int)right.getClientProperty("enabled") == 0)){
getNumberSmall(right);
right.putClientProperty("enabled", 1);
}
if((left != null ) && ((int)left.getClientProperty("enabled") == 0)){
getNumberSmall(left);
left.putClientProperty("enabled", 1);
}
if((DRight != null) && ((int)DRight.getClientProperty("enabled") == 0)){
getNumberSmall(DRight);
DRight.putClientProperty("enabled", 1);
}
if((DLeft != null) && ((int)DLeft.getClientProperty("enabled") == 0)){
getNumberSmall(DLeft);
DLeft.putClientProperty("enabled", 1);
}
if((DRightD != null) && ((int)DRightD.getClientProperty("enabled") == 0)){
getNumberSmall(DRightD);
DRightD.putClientProperty("enabled", 1);
}
if((DLeftD != null) && ((int)DLeftD.getClientProperty("enabled") == 0)){
getNumberSmall(DLeftD);
DLeftD.putClientProperty("enabled", 1);
}
}
else{
String number = "" + mNum;
clicked.setText(number);
}
}
答案 0 :(得分:0)
似乎没有在点击按钮上启用设置,因此没有任何操作可以阻止循环中的递归。
例如:点击 - &gt;以上 - &gt;下面 - &gt;以上......
答案 1 :(得分:0)
getNumberSmall(above);
above.putClientProperty("enabled", 1);
这里,在getNumberSmall
之前putClientProperty
将导致程序调用{{1}}使用指定的按钮,而不会对正在反映的按钮进行任何更改,这会导致递归继续直到StackOverflowError被扔了。如果切换两个方法调用,JButton的指定属性将在按钮作为参数传递之前更改,并且此更改将反映在JButton参数上。