单击时尝试获取JButton的坐标

时间:2022-10-10 19:10:44

I'm using the following code in order to create a grid of JButtons and change their color on click. The next step I want to do is be able to compare this grid with other grids like it. I have been trying to get the coordinates of the JButton when clicked, (x, y), but have been unable to find a way to do so. Thanks for any help in advance!

我正在使用以下代码来创建JButtons网格并在点击时更改它们的颜色。我想要做的下一步是能够将此网格与其他网格进行比较。单击时,我一直试图获取JButton的坐标,(x,y),但一直无法找到方法。在此先感谢您的帮助!

public class ButtonGrid {

  JFrame frame = new JFrame(); //creates frame
  JButton[][] grid; //names the grid of buttons
  HashMap<JButton, String> state;
  static int WIDTH = 8;
  static int LENGTH = 8;

  public ButtonGrid(int width, int length) { //constructor
    frame.setLayout(new GridLayout(width,length)); //set layout
    grid = new JButton[width][length]; //allocate the size of grid
    state = new HashMap<JButton, String>();

    for(int y = 0; y < length; y++) {
      for(int x = 0; x < width; x++) {
        final JButton nb = new JButton();//new ButtonColor; //creates a button
        nb.setPreferredSize(new Dimension(50, 50));
        grid[x][y] = nb;
        state.put(grid[x][y], "blank");

        nb.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if(state.get(nb).equals("blank"))
              mapButtonToColor(nb, "red");
            else if(state.get(nb).equals("red"))
              mapButtonToColor(nb, "blank");
            setButtonColors();
          }
        });
        frame.add(grid[x][y]); //adds new button to grid
      }
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack(); //sets appropriate size for frame
    frame.setVisible(true); //makes frame visible
  }

  public static void main(String[] args) {
    new ButtonGrid(WIDTH, LENGTH);
  }

  public void mapButtonToColor(JButton b, String c) {
    state.put(b, c);
  }

  public void setButtonColors() {
    for(JButton b  : state.keySet()) {
      Color c = state.get(b).equals("red") ? Color.black : Color.white;
      b.setBackground(c);
    }
  }
}

2 个解决方案

#1


-1  

maybe this will help you:

也许这会对你有所帮助:

when a button is pressed in the said grid, simple iterate it to find the button that was clicked, then return the coordinates, something like this:

当在所述网格中按下按钮时,简单地迭代它以找到被点击的按钮,然后返回坐标,如下所示:

private class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        for(int i = 0;i< grid.length; i++){
            for(int j = 0;j < grid.length;j++){
                if(source == grid[i][j]){
                    JButton clicked == grid[i][j];
                    // do something with this 
                }
            }
        }
    }
}

#2


0  

Can you not just implement a MouseListener?

你能不能只实现一个MouseListener?

yourButton.addMouseListener(new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
          int x=e.getX();
          int y=e.getY();
          System.out.println(x+","+y);
    }
 });

This should do what you need as far as getting coordinates whenever your button is clicked. Also you could set the listener on your JFrame component to find the coordinate for every click (if that is needed).

只要在单击按钮时获取坐标,就可以执行所需操作。您还可以在JFrame组件上设置侦听器,以查找每次单击的坐标(如果需要)。

#1


-1  

maybe this will help you:

也许这会对你有所帮助:

when a button is pressed in the said grid, simple iterate it to find the button that was clicked, then return the coordinates, something like this:

当在所述网格中按下按钮时,简单地迭代它以找到被点击的按钮,然后返回坐标,如下所示:

private class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        for(int i = 0;i< grid.length; i++){
            for(int j = 0;j < grid.length;j++){
                if(source == grid[i][j]){
                    JButton clicked == grid[i][j];
                    // do something with this 
                }
            }
        }
    }
}

#2


0  

Can you not just implement a MouseListener?

你能不能只实现一个MouseListener?

yourButton.addMouseListener(new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
          int x=e.getX();
          int y=e.getY();
          System.out.println(x+","+y);
    }
 });

This should do what you need as far as getting coordinates whenever your button is clicked. Also you could set the listener on your JFrame component to find the coordinate for every click (if that is needed).

只要在单击按钮时获取坐标,就可以执行所需操作。您还可以在JFrame组件上设置侦听器,以查找每次单击的坐标(如果需要)。