【发布时间】:2013-04-30 21:34:42
【问题描述】:
我正在尝试在 java 上制作幻灯片拼图。现在我有一个小问题。我想知道我单击的按钮的索引,它位于ArrayList 中。
ArrayList<JButton> 按钮,包含多个JButtons,我在每个按钮上添加了一个ActionListener,然后再放入这个ArrayList。
我做错了什么?
这里有一些代码作为参考:
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");
}
}
【问题讨论】: