Comparing components via setName() .

后端 未结 2 591
春和景丽
春和景丽 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:42

    JButton uses an ActionListener to trigger notifications back to your program to indicate when it's been triggered. This allows the button to respond to different types of event, including the mouse, keyboard and program triggers.

    As apart of the action API, you can supply an action command for each button. See JButton#setActionCommand

    Basically you would integrate it in a simular way to your mouse listener...

    public void actio Performed(ActionEvent evt) {
        if (command.equals(evt.getActionCommand()) {...}
    }
    

    Depending on your requirements, it might even be easier to use the Action API

    The problem you are actually having is waa is an array, therefore, it doesn't have a getName method. I'm also unclear as to why you have two arrays of buttons?

提交回复
热议问题