我在GridLayout中获取按钮的X和Y索引的方法

时间:2014-01-25 03:35:58

标签: java swing jbutton java-7 layout-manager

这与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;
    }
}

示例GUI:

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();
            }
        });         

    }

}

现在我的问题:

  1. 我在这里重新发明了轮子吗?我的意思是,是否有更简单的方法来实现同样的目标?

  2. 每次我们需要找到坐标时,它是否都不如搜索数组?

1 个答案:

答案 0 :(得分:1)

version使用的扩展程序的原始example

GridButton extends JButton

更新的version取决于所见的座谈会here。虽然在某些情况下延伸可能是适当的,但提到了一些替代方案here;客户端属性特别方便。从网格坐标中识别按钮也很容易:

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);
}