这是我的代码,我想知道每次单击实例框架中的现有按钮之一时是否可以将新按钮添加到网格布局中。
public class Board {
public static void main(String[] args) {
JButton[] button = new JButton[40];
int i = 0;
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(20, 20, 15, 15));
while (i < 40) {
button[i] = new JButton("button" + i);
button[i].addActionListener(new Action());
frame.add(button[i]);
i++;
}
frame.setSize(700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class Action implements ActionListener{
@Override
public void actionPerformed (ActionEvent e){
}
}
}