JButtons 不遵循预期的处理程序?
JButtons don't follow the intended handlers?
我正在制作一个扫雷游戏,在第一部分,我通过使用 boolean1
来决定某个按钮是否有炸弹(该字段是一个 16x16 数组)我有测试了这部分,输出是正确的。 50 个随机 true
值,其余为 false
我的问题从第二部分开始,我想根据 boolean1
的值通过按钮获得特定操作。实现代码时,所有 jbuttons
都遵循第二个 ActionListener
,其中图标设置为 bomb
我想让 jbuttons
也遵循第一个处理程序。
第一步
static void placeMines()
{
for (int x=0;x<16;x++)
{
for (int y=0;y<16;y++)
{
if(boolean1[x][y]=(true))
{
boolean1[x][y]=false;
}
}
}
int minesPlaced = 0;
Random random = new Random();
while(minesPlaced < 50)
{
int a = random.nextInt(Width);
int b = random.nextInt(Height);
boolean1[a][b]=(true);
minesPlaced ++;
}
}
第二步:
static void buttonfunctions()
{
for(int c=0;c<16;c++)
{
for(int d=0;d<16;d++)
{
if (boolean1[c][d]=false)
{
final int temp3=c;
final int temp4=d;
jbuttons[c][d].addActionListener(new ActionListener()
{
@Override
public void actionPerformed (ActionEvent e)
{
jbuttons[temp3][temp4].setIcon(clickedCell);
}
});
}
if(boolean1[c][d]=true)
{
final int temp1=c;
final int temp2=d;
jbuttons[temp1][temp2].addActionListener(new ActionListener()
{
@Override
public void actionPerformed (ActionEvent e)
{
jbuttons[temp1][temp2].setIcon(bomb);
}
});
}
}
}
}
为了检查一个布尔值是否为真,你想做:
if (myBoolean)
正在做
if (myBoolean == true)
是等效的,但比需要的更冗长。
正在做
if(myBoolean = true)在语法上是正确的,但是它的作用是将true赋给myBoolean,然后评估赋值的结果,即true
。所以,回到你的代码:
如果下面代码的目的是重置矩阵:
if(boolean1[x][y]=(true))
{
boolean1[x][y]=false;
}
那你就应该做
boolean1[x][y] = false;
还有
if (boolean1[c][d]=false)
大概应该是:
if (! boolean1[c][d])
您的代码可能存在更多问题,但您可能需要开始修复此问题。
我正在制作一个扫雷游戏,在第一部分,我通过使用 boolean1
来决定某个按钮是否有炸弹(该字段是一个 16x16 数组)我有测试了这部分,输出是正确的。 50 个随机 true
值,其余为 false
我的问题从第二部分开始,我想根据 boolean1
的值通过按钮获得特定操作。实现代码时,所有 jbuttons
都遵循第二个 ActionListener
,其中图标设置为 bomb
我想让 jbuttons
也遵循第一个处理程序。
第一步
static void placeMines()
{
for (int x=0;x<16;x++)
{
for (int y=0;y<16;y++)
{
if(boolean1[x][y]=(true))
{
boolean1[x][y]=false;
}
}
}
int minesPlaced = 0;
Random random = new Random();
while(minesPlaced < 50)
{
int a = random.nextInt(Width);
int b = random.nextInt(Height);
boolean1[a][b]=(true);
minesPlaced ++;
}
}
第二步:
static void buttonfunctions()
{
for(int c=0;c<16;c++)
{
for(int d=0;d<16;d++)
{
if (boolean1[c][d]=false)
{
final int temp3=c;
final int temp4=d;
jbuttons[c][d].addActionListener(new ActionListener()
{
@Override
public void actionPerformed (ActionEvent e)
{
jbuttons[temp3][temp4].setIcon(clickedCell);
}
});
}
if(boolean1[c][d]=true)
{
final int temp1=c;
final int temp2=d;
jbuttons[temp1][temp2].addActionListener(new ActionListener()
{
@Override
public void actionPerformed (ActionEvent e)
{
jbuttons[temp1][temp2].setIcon(bomb);
}
});
}
}
}
}
为了检查一个布尔值是否为真,你想做:
if (myBoolean)
正在做
if (myBoolean == true)
是等效的,但比需要的更冗长。
正在做
if(myBoolean = true)在语法上是正确的,但是它的作用是将true赋给myBoolean,然后评估赋值的结果,即true
。所以,回到你的代码:
如果下面代码的目的是重置矩阵:
if(boolean1[x][y]=(true))
{
boolean1[x][y]=false;
}
那你就应该做
boolean1[x][y] = false;
还有
if (boolean1[c][d]=false)
大概应该是:
if (! boolean1[c][d])
您的代码可能存在更多问题,但您可能需要开始修复此问题。