获取gridlayout上按钮的位置

如何获得带有gridlayout的点击按钮的位置(我的意思是行和列)?

public void init(final Container pane) { JPanel controls = new JPanel(); int size = (int) Math.sqrt(puzzle.getSize() + 1); controls.setLayout(new GridLayout(size, size)); for (int i = 0; i < puzzle.getSize(); i++) { int k = puzzle.getListItem(i); if (k == puzzle.getEmptyFlag()) controls.add(new JLabel("")); else { JButton jb = new JButton(String.valueOf(k)); jb.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { //get row and column } }); controls.add(jb); } } pane.add(controls); } 

  1. 切勿在JButton上添加MouseListener用于此目的。 请改用ActionListener。
  2. 为什么不创建JButton的数组或ArrayList,然后简单地遍历列表以找到合适的数组?

 private JButton[][] buttonGrid = new JButton[ROWS][COLS]; 

在其他地方,您需要使用可行的JButton对象填充网格,并将这些JButton放入GUI中。

然后在程序中使用嵌套for循环遍历网格,比较网格按钮和getSource() JButton。

即在JButton的ActionListener中

 public void actionPerformed(ActionEvent e) { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if buttonGrid[row][col] == e.getSource(); // here you have your row and column } } } 

编辑
你问:

  1. 为什么?

因为在许多情况下它无法正常工作。 ActionListeners已经构建为专门用于JButtons和JMenuItems,并且具有使这个function很好地工作的机制。 例如,假设您决定只在用户填充两个JTextField后setEnabled(boolean enabled) JButton,并且使用JButton的setEnabled(boolean enabled)方法执行此操作,禁用JButton将不会阻止MouseListener工作,但它将停止ActionListener。


编辑2

例如,

 import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class ButtonGridEg extends JPanel { private static final int ROWS = 8; private static final int COLS = ROWS; private static final int GAP = 5; private JButton[][] buttonGrid = new JButton[ROWS][COLS]; public ButtonGridEg() { setLayout(new GridLayout(ROWS, COLS, GAP, GAP)); ActionListener buttonListener = new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { JButton selectedBtn = (JButton) evt.getSource(); for (int row = 0; row < buttonGrid.length; row++) { for (int col = 0; col < buttonGrid[row].length; col++) { if (buttonGrid[row][col] == selectedBtn) { System.out.printf("Selected row and column: %d %d%n", row, col); } } } } }; for (int row = 0; row < buttonGrid.length; row++) { for (int col = 0; col < buttonGrid[row].length; col++) { String text = String.format("Button [%d, %d]", row, col); buttonGrid[row][col] = new JButton(text); buttonGrid[row][col].addActionListener(buttonListener); add(buttonGrid[row][col]); } } } private static void createAndShowGui() { ButtonGridEg mainPanel = new ButtonGridEg(); JFrame frame = new JFrame("ButtonGridEg"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } 

在这种情况下,您甚至不必搜索索引,因为您在创建按钮时知道它们:

 for (int i = 0; i < puzzle.getSize(); i++) { int k = puzzle.getListItem(i); if (k == puzzle.getEmptyFlag()) controls.add(new JLabel("")); else { JButton jb = new JButton(String.valueOf(k)); final int rowIndex = i / size; final int columnIndex = i % size; // Using an ActionListener, as Hovercraft Full Of Eels already told you: jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("rowIndex "+rowIndex+" columnIndex "+columnIndex); } }); controls.add(jb); } }