这与How to get X and Y index of element inside GridLayout?帖子及其答案有关。
不管出于什么原因,他们都不建议将JButton扩展到包括它在网格和相关按钮数组中的位置。
我制作了下面的插图,当单击按钮时,它只显示按钮的坐标。
扩展JButton

package buttons_array;

import javax.swing.*;

@SuppressWarnings("serial")
public class ButtonWithCoordinates extends JButton {

    int coordX;
    int coordY;

    public ButtonWithCoordinates(String buttonText, int coordX, int coordY) {
        super(buttonText);
        this.coordX = coordX;
        this.coordY = coordY;
    }

    /**
     * @return the coordX
     */
    public int getCoordX() {
        return coordX;
    }

    /**
     * @return the coordY
     */
    public int getCoordY() {
        return coordY;
    }
}

图形用户界面示例:
package buttons_array;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonsArray implements ActionListener {

    private ButtonWithCoordinates buttons[][];
    private int nRows;
    private int nCols;

    private JFrame frame;
    private JPanel panel;

    public ButtonsArray(int x, int y) {
        if (x > 0 && y > 0) {
            nRows = x;
            nCols = y;
            buttons = new ButtonWithCoordinates[nRows][nCols];
            for (int i=0; i < nRows; ++i) {
                for (int j=0; j < nCols; ++j) {
                    buttons[i][j] = new ButtonWithCoordinates("        ", i, j);
                    buttons[i][j].addActionListener(this);
                }
            }
        } else {
            throw new IllegalArgumentException("Illegal array dimensions!!!");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub
        ButtonWithCoordinates button = (ButtonWithCoordinates) e.getSource();
        button.setText(button.getCoordX() + ", " + button.getCoordY());

    }

    public void GUI() {

        if (buttons == null) { throw new NullPointerException("Array is not initialized!!!"); }

        frame = new JFrame();
        panel = new JPanel();

        frame.setContentPane(panel);
        panel.setLayout(new GridLayout(nRows, nCols));
        for (int i=0; i < nRows; ++i) {
            for (int j=0; j < nCols; ++j) {
                panel.add(buttons[i][j]);
            }
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ButtonsArray(3, 5).GUI();
            }
        });

    }

}

现在我的问题是:
我在这里重新发明轮子了吗?我的意思是,有没有更直接的方法来达到同样的效果?
每次我们需要寻找坐标时,它在任何方面都比搜索数组差吗?

最佳答案

原来的version使用了扩展名:

GridButton extends JButton

更新后的example是基于所见的口语version。虽然扩展在某些情况下可能是合适的,但是提到了一些替代方案;客户机属性特别方便。从网格坐标中识别按钮也很容易:
private static final int N = 5;
List<JButton> list = new ArrayList<>();
…
private JButton getGridButton(int r, int c) {
    int index = r * N + c;
    return list.get(index);
}

10-07 19:46
查看更多