一个简单的方法是创建一个ActionListener
您自己的类,在创建这些按钮时将行和列传递给它。当他们的动作被触发时,他们可以将其传递给您所指的函数。像这样的东西:
package com.sandbox;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Sandbox {
public static void main(String[] args) {
Container pane = //...
pane.setLayout(new GridLayout(10, 10));
for (int r = 0; r < 10; r++) {
for (int c = 0; c < 10; c++) {
JButton button = new JButton();
button.addActionListener(new PositionAwareActionListener(r, c));
pane.add(button);
}
}
}
private static class PositionAwareActionListener implements ActionListener {
private int r;
private int c;
public PositionAwareActionListener(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public void actionPerformed(ActionEvent e) {
//callYourFunction(r, c);
}
}
}