What you want to do is to set up a listener at the beginning, of the program, not on a call to playerMove
. So something like this
public static JButton [][] b = new JButton[3][3];
{ // Initialization code follows
b[0][0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = b[0][0].getText();
if(text.equals("-")){
b[0][0].setText("O");
computerMove();
}
else{
System.out.println("Pick Again");
} } });
// And so on for the other 8 buttons.
}
Of course you probably would want to use a loop rather than repeating similar code 9 times, but, as you say, that's another issue.