0
我試圖在java上製作幻燈片拼圖。現在我有一個小問題。我想知道我點擊的按鈕的索引,位於ArrayList
。如何獲取ArrayList中的ActionEvent的來源
ArrayList<JButton>
的按鈕,包含許多JButton
S,I將其放入此ArrayList
之前加入一個ActionListener到每個按鈕。
我在做什麼錯?
下面是一些代碼作爲參考:
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");
}
}