How can I move the board?

I am making a board game and cannot move it. The piece will move according to the result of the bone. The following is what I'm trying to do, but it does not work. (From the [] button. AddActionListener (new ActionListener ()) Note: I used ImageIcon to represent my works. Any help?

    //Puts the player 1 piece on button 1,3,5,7,9 and player 2 piece on button 2,4,6,8,10 
    if ((btnNumber - 1) < 10) 
    { 
        if (((btnNumber - 1) % 2) == 0) 
        { 
            buttons[btnNumber - 1].setIcon(piece1); 
        } 
        else 
        { 
            buttons[btnNumber - 1].setIcon(piece2); 
        } 
    } 
    centerPanel.add(buttons[btnNumber - 1]); 
} 

frame.add(centerPanel, BorderLayout.CENTER); 
+3
source share
1 answer

It seems that there are some basic things that you still do not understand. Here are some tips to get closer to your goal:

  • button[].addActionListener- This is a meaningless statement. You cannot add an action listener immediately to an entire array. Perhaps you wanted to say buttons[btnNumber - 1].addActionListenerand put it inside the loop for.
  • buttons[]==ImageIcon("piece1") ( ). buttons[btnNumber - 1] == ImageIcon("piece1"), (# 3).
  • , , piece1Location piece2Location, . , , if (btnNumber - 1 == piece1Location)
  • Java 0. Java for - 0, <= <. for(int i = 0; i < 30; i++) btnNumber - 1.
  • . , () , ( , ..). .
+3

All Articles