I want to build a 2d array of toggle buttons in Java that when I click on a button, it passes from green to red, how can I do this? The following code creates the array of buttons.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Container implements ActionListener {
private static JFrame container;
private static JToggleButton[][] butoes;
public static void main(String args[]) {
container = new JFrame("Game of Life");
butoes = new JToggleButton[20][20];
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
butoes[i][j] = new JToggleButton();
}
}
container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container.setLayout(new GridLayout(20, 20));
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
container.add(butoes[i][j]);
}
}
container.pack();
container.setSize(700, 700);
container.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent evento) {
}
}