codice:
for (i=0; i<n; i++){
for (y=0; y<m; y++){
button = new JButton(""+x+y);
button.addActionListener(new ButtonListener());
//oppure puoi crearne una sola istanza e passarla a tutti i bottoni.
panel.add(button);
}
}
.....
.....
private class ButtonListener extends ActionListener{
public void actionPerformed(ActionEvent e){
JButton source=(JButton)e.getSource();
if(source.getBackground()!=Color.RED)
source.setBackground(Color.RED);
else
source.setBackground(Color.GREEN);//Metti il colore che vuoi :)
}
}
potresti fare una cosa del genere