meow00
asked on
addActionListener ....
Hi Experts,
I have a question about the following code :
at line 00, does "this" refer to a PlayGame object or the mineButton[i] ?
so .... must an "addActionListener" work with an actionPerfomed() method ? thanks !
-------------------------
public PlayGame(){
System.out.print(ROW_NUM + " " + COL_NUM) ;
JPanel minePanel = new JPanel() ;
minePanel.setLayout(new GridLayout(ROW_NUM,COL_NUM )) ;
for(int i=0 ; i < ROW_NUM*COL_NUM; i++){ mineButton[i] = new Mine(i) ;
mineButton[i].addActionLis tener(this ); // line 00
minePanel.add(mineButton[i ]) ;
}
I have a question about the following code :
at line 00, does "this" refer to a PlayGame object or the mineButton[i] ?
so .... must an "addActionListener" work with an actionPerfomed() method ? thanks !
-------------------------
public PlayGame(){
System.out.print(ROW_NUM + " " + COL_NUM) ;
JPanel minePanel = new JPanel() ;
minePanel.setLayout(new GridLayout(ROW_NUM,COL_NUM
for(int i=0 ; i < ROW_NUM*COL_NUM; i++){ mineButton[i] = new Mine(i) ;
mineButton[i].addActionLis
minePanel.add(mineButton[i
}
this always refers to the current instance of the class it is in, in this case the PlayGame class.
So you add an ActionListener to the mineButton[i].
And that ActionListener is an instance of PlayGame.
To be able to do that, your PlayGame class must implement the ActionListener interface.
That's rather easy: it should implement the actionPerformed function:
public class PlayGame implements ActionListener {
public void actionPerformed(ActionEven t evt) {
}
}
So you add an ActionListener to the mineButton[i].
And that ActionListener is an instance of PlayGame.
To be able to do that, your PlayGame class must implement the ActionListener interface.
That's rather easy: it should implement the actionPerformed function:
public class PlayGame implements ActionListener {
public void actionPerformed(ActionEven
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks
:-)
So when calling addActionListener(this), your basically saying that the current ActionListener is implemented in the PlayGame object.
Therefore you'll need to make sure you do implement ActionListener for everything to work ok.