a.s
a.s

Reputation: 31

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. 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. When implementing the code, all of the jbuttonsfollow the second ActionListener where the icon is set to bomb I want to get the jbuttons to also follow the first handler.

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

            }   
        }

}

Upvotes: 0

Views: 43

Answers (1)

Roberto Attias
Roberto Attias

Reputation: 1903

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. 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.

Upvotes: 2

Related Questions