我正在尝试在 java 上制作幻灯片拼图。现在我有一个小问题。我想知道我单击的按钮的索引,它位于ArrayList
.
ArrayList<JButton>
按钮,包含许多s,我在JButton
每个按钮中添加了一个 ActionListener,然后再将其放入 thisArrayList
中。
我究竟做错了什么?
这是一些代码作为参考:
public void actionPerformed(ActionEvent ae)
{
if (buttons.contains(ae.getSource()) == true)
{
int click = buttons.indexOf(ae.getSource()); // <--- What's wrong with this?
System.out.println(click); /* I checked the index I got by printing
it out as a test, and it always gives
me the Integer '0', even if I clicked
the 9th Button for example. */
}
else
{
System.out.println("No click");
}
}