2013-04-30 2 views
0

자바에서 슬라이드 퍼즐을 만들려고합니다. 이제 조금 문제가 있습니다. 클릭 한 버튼의 인덱스를 알고 싶습니다.이 버튼은 ArrayList에 있습니다.ArrayList에서 ActionEvent의 소스를 얻는 방법

ArrayList<JButton> 버튼이 있으며 JButton이 포함되어 있습니다.이 버튼을 넣기 전에 각 버튼에 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"); 
    } 
} 

답변

0

각 버튼에 클라이언트 속성 설정 수행 할 수 있습니다 무엇 : 액션 청취자 사용자의 다음

JButton one = new JButton("1"); 
one.putClientProperty("Which", new Integer(1)); 

:

JButton button = ae.getSource(); 
int value = button.getClientProperty("Which").intValue(); 

일부 캐스트를 추가해야하고 일부 오류 검사가 필요하지만 그 이유는 ...

관련 문제