简体   繁体   English

JButton不遵循预期的处理程序?

[英]JButtons don't follow the intended handlers?

I'm making a minesweeper game, in the first part, I'm deciding whether or not there is a bomb at a certain button by using boolean1 (the field is a 16x16 array) I have tested this part, and the output is correct. 我正在做一个扫雷游戏,在第一部分中,我通过使用boolean1 (该字段是16x16数组)来确定某个按钮上是否存在炸弹,我已经测试了这一部分,并且输出正确。 50 random true values and the rest are false my problem starts at the second part, where I want to get a certain action by the button based on the value of the boolean1 . 50个随机的true值,其余的为false我的问题从第二部分开始,我想根据boolean1的值通过按钮执行某些操作。 When implementing the code, all of the jbuttons follow the second ActionListener where the icon is set to bomb I want to get the jbuttons to also follow the first handler. 实现代码时,所有jbuttons跟随第二个ActionListener ,其中图标设置为bomb我想让jbuttons也跟随第一个处理程序。

1st procedure 第一步

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 ++;
                }
        }

2nd procedure: 第二程序:

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);                   
                                }
                        });
                }

            }   
        }

}

In order to check if a boolean is true, you want to do : 为了检查布尔值是否为真,您需要执行以下操作:

if (myBoolean)

doing 在做

if (myBoolean == true) 

is equivalent, but more verbose than needed. 是等效的,但比所需的更为冗长。

doing 在做

if (myBoolean = true) is syntactically correct, but has the effect of assigning true to myBoolean, and then evaluating the result of the assignment, which is true . if(myBoolean = true)在语法上是正确的,但是具有将true分配给myBoolean,然后评估分配结果true So, going back to your code: 因此,回到您的代码:

If the intent of the following code is to reset the matrix: 如果以下代码的目的是重置矩阵:

   if(boolean1[x][y]=(true))
   {
      boolean1[x][y]=false;
   }

then you should just do 那你应该做

boolean1[x][y] = false;

Also

if (boolean1[c][d]=false)

should probably be: 应该可能是:

if (! boolean1[c][d]) 

There may be more stuff wrong with your code, but you may want to start fixing this. 您的代码可能还有更多问题,但是您可能要开始解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM