我有一個類Game
和一個按鈕陣列buts
,我想調用Game類fun(i,j)
當任何buts [i] [j]被點擊,我試過像這樣:設置onclickListener爲陣列按鈕上的按鈕的最佳做法
buts = new JButton[Setting.length][Setting.width];
Game game = new Game(setting);
int hgap = 4, vgap = 4;
panel = new JPanel(new GridLayout(Setting.length, Setting.width, hgap, vgap));
for (int i = 0; i < Setting.length; i++) {
for (int j = 0; j < Setting.width; j++) {
//JButton btn = new JButton();
buts[i][j] = new JButton();
buts[i][j].setText(String.valueOf(j));
setColor(buts[i][j], Game.cells[i][j].getColor());
buts[i][j].addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
panel.add(buts[i][j]);
}
}
private void buttonActionPerformed(ActionEvent evt) {
}
,但我怎樣才能發送I,J的功能buttonActionPerformed
?