【问题标题】:How do I tell which button is being clicked in an array of buttons? [duplicate]如何判断在一组按钮中单击了哪个按钮? [复制]
【发布时间】:2012-11-12 22:52:13
【问题描述】:

可能重复:
How to get X and Y index of element inside GridLayout?

我有一个想要使用的二维按钮数组。当我想调用 actionListener 时,如何判断我的这个 2D 数组中的哪个按钮索引被单击?这是我第一次与听众打交道,所以如果可以的话,请在更基本的层面上解释一下。

这里是一些关于如何在网格 (12x12) 上布置按钮的代码

//A loop to add a new button to each section of the board grid.
for (int i = 0; i < gridSize; i++) {
  for (int j = 0; j < gridSize; j++) {
    gameButtons[i][j] = new JButton();
    gameButtons[i][j].setBackground(colors[(int)(Math.random() * numColors)]);
    boardGrid.add(gameButtons[i][j]);

    try {
      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch (Exception e) {
    }

  }
}

这些按钮从之前创建的一组颜色中随机分配一种颜色。我现在必须覆盖 actionlistener,但我不知道如何以一种允许我按下按钮并将其与周围的其他按钮进行比较的方式来做到这一点。我想提一下,我正在处理静态方法。

【问题讨论】:

标签: java arrays swing jbutton actionlistener


【解决方案1】:

首先你应该通过addActionListener()这个方法注册你所有的按钮到一个actionlistener。然后在actionPerformed() 方法中,您应该调用getSource() 来获取对被点击按钮的引用。

查看post

代码在这里,gameButtons[][] 数组必须是全局可用的

//A loop to add a new button to each section of the board grid.
for (int i = 0; i < gridSize; i++) 
{
  for (int j = 0; j < gridSize; j++) 
  {
    gameButtons[i][j] = new JButton();
    gameButtons[i][j].addActionListener(this);
    gameButtons[i][j].setBackground(colors[(int)(Math.random() * numColors)]);
    boardGrid.add(gameButtons[i][j]);

    try {
    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) { } 
  }
}

//--------------------------------------------------------


@Override
public void actionPerformed(ActionEvent ae)
{
  for (int i = 0; i < gridSize; i++) 
  {
    for (int j = 0; j < gridSize; j++) 
     {
       if(ae.getSource()==gameButtons[i][j]) //gameButtons[i][j] was clicked
       {
             //Your code here
       }
     }
  }
}

【讨论】:

  • 我很抱歉,我忘了提到我在一个静态类中这样做,所以使用“this”是行不通的。即使我在静态方法中而不更改方法,有没有办法做到这一点?
【解决方案2】:

如果您想避免再次循环遍历数组,也可以将索引存储在 JButton 中。

JButton button = new JButton();
button.putClientProperty( "firstIndex", new Integer( i ) );
button.putClientProperty( "secondIndex", new Integer( j ) );

然后在你的ActionListener

JButton button = (JButton) actionEvent.getSource();
Integer firstIndex = button.getClientProperty( "firstIndex" );
Integer secondIndex = button.getClientProperty( "secondIndex" );

【讨论】:

  • 谢谢。这个解决方案看起来比使用两个循环更优化。
【解决方案3】:

如果你需要按下按钮的索引,试试这个:

private Point getPressedButton(ActionEvent evt){
    Object source = evt.getSource();
    for(int i = 0; i < buttons.length; i++){
        for(int j = 0; j < buttons[i].length; j++){
            if(buttons[i][j] == source)
                return new Point(i,j);
        }
    }
    return null;
}

然后你可以通过

提取值
Point p = getPressedButton(evt);

这意味着:

按下按钮 == 按钮[p.x][p.y]

否则,一个简单的调用 evt.getSource(); 就可以完成这项工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 2016-06-03
    相关资源
    最近更新 更多