我在GridLayout中获取按钮的X和Y索引的方法 [英] My way to get X and Y index of buttons inside GridLayout

查看:368
本文介绍了我在GridLayout中获取按钮的X和Y索引的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与如何获取有关GridLayout内部元素的X和Y索引?帖子及其答案。

无论出于何种原因,他们都没有建议扩展 JButton 包括它在网格和相关数组按钮中的位置。

For whatever reason none of them suggested to extend JButton to include its position in the grid and in associated array of buttons.

我做了下面的插图,它只是在点击时显示按钮的坐标。

I have made the following illustration that simply displays button's coordinates when it's clicked.

扩展 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. 我是否一直在这里重新发明轮子?我的意思是,是否有更简单的方法来实现相同的目标?

  1. Have I been reinventing the wheel here? I mean, is there a more straightforward way to achieve the same?

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

Is it in any way inferior to searching through the array each time we need to find the coordinates?


推荐答案

原始示例使用扩展程序的stackoverflow.com/revisions/7706684/1>版本:

The original version of this example used extension:

GridButton extends JButton

更新的版本取决于所见的讨论此处。虽然在某些情况下扩展可能是合适的,但此处提及了一些替代方案。客户端属性特别方便。从网格坐标中识别按钮也很容易:

The updated version was predicated on the colloquy seen here. While extension may be appropriate in some contexts, a few alternatives are mentioned here; a client property is particularly convenient. Identifying a button from its grid coordinates is also easy:

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

这篇关于我在GridLayout中获取按钮的X和Y索引的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆