Comparing components via setName() .

后端 未结 2 625
春和景丽
春和景丽 2021-01-28 04:13

I am coding an image puzzle game and one part of the code is to compare the pieces the user has selected to the pieces of the correct image.

Each image piece is already

2条回答
  •  遇见更好的自我
    2021-01-28 04:41

    In your mouseReleased event m.getButton() is returning the mouse button that was clicked. You'll want to do something more like this that will get you closer:

    if (m.getComponent().getName().equals(waa.getName())) {
    

    m.getComponent() returns the Component object (your JButton) that the event was fired from. From there you can do the comparison with the getName approach you are using.

    There's an additional issue in that your waa variable is an array. I'm not sure how you want to compare them, whether running through the arrays and making sure the index and names match, but that's an additional issue you need to look into.

提交回复
热议问题