java 如何判断按钮数组中的哪个按钮被点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13548299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I tell which button is being clicked in an array of buttons?
提问by David
Possible Duplicate:
How to get X and Y index of element inside GridLayout?
I have an 2D array of buttons that I wish to use. When I want to call an actionListener, how to I tell which button index in this 2D array of mine is being clicked? This is my first time dealing with a listener, so please explain this on a more basic level if you can.
我有一个想要使用的二维按钮阵列。当我想调用 actionListener 时,如何判断我的这个 2D 数组中的哪个按钮索引被点击?这是我第一次与听众打交道,所以如果可以的话,请在更基本的层面上解释这一点。
Here is some code of how I have my buttons laid out on a gride (12x12)
这是我如何在网格(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) {
}
}
}
These buttons are randomly assigned a color from an array of colors created earlier. I now have to override actionlistener and I don't know how to do that in a way which allows me to get the button being pressed and compare it to other buttons around it. I would like to mention that I am dealing with static methods.
这些按钮从之前创建的一系列颜色中随机分配一种颜色。我现在必须覆盖 actionlistener,但我不知道如何以一种允许我按下按钮并将其与周围的其他按钮进行比较的方式来执行此操作。我想提一下,我正在处理静态方法。
回答by Extreme Coders
First of all you should register all of your buttons with an actionlistener by this method addActionListener()
. Then within the actionPerformed()
method you should call getSource()
to get a reference to the button which was clicked.
首先,您应该通过此方法向 actionlistener 注册所有按钮addActionListener()
。然后在该actionPerformed()
方法中,您应该调用getSource()
以获取对单击的按钮的引用。
Check this post
检查这个帖子
Anyway here is the code, The gameButtons[][] array has to be available globally
无论如何这里是代码, 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
}
}
}
}
回答by Robin
If you want to avoid looping over the arrays again, you can store the indices in the JButton
as well.
如果您想避免再次循环遍历数组,您也可以将索引存储在 中JButton
。
JButton button = new JButton();
button.putClientProperty( "firstIndex", new Integer( i ) );
button.putClientProperty( "secondIndex", new Integer( j ) );
and then in your ActionListener
然后在你的 ActionListener
JButton button = (JButton) actionEvent.getSource();
Integer firstIndex = button.getClientProperty( "firstIndex" );
Integer secondIndex = button.getClientProperty( "secondIndex" );
回答by Mordechai
if you need the indexof the pressed button, try this:
如果您需要按下按钮的索引,请尝试以下操作:
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;
}
Then you can extract the value by
然后您可以通过以下方式提取值
Point p = getPressedButton(evt);
And this means:
这意味着:
Button pressed == buttons[p.x][p.y]
按钮按下 == 按钮 [px][py]
Otherwise, a simple call evt.getSource();
does the job.
否则,一个简单的调用evt.getSource();
就可以完成这项工作。