2012-04-29 2 views
0

목록의 각 버튼에 대해 ActionListener를 수행하려면 어떻게해야합니까? 나는 JGraphMenu &을 사용하여 각 항목에 대한 경고를 표시하려고합니다.JGraphMenu를 사용하는 메뉴 항목의 ActionListener

public static void main(String[] args){ 
JFrame f = new JFrame(); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
List<String> MenuItem = new ArrayList<String>(); 
MenuItem.add("Choose The XML File"); 
MenuItem.add("Generate the tree"); 
MenuItem.add("Generate the latex code"); 
MenuItem.add("Generate automata"); 
List<ActionListener> listeners = new ArrayList<ActionListener(); 
for(int i=0; i<4; i++){ 
listeners.add(null); 
} 

JGraphMenu menu = new JGraphMenu(
ConstantesStyles.NOIR, 
JGraphMenu.VERTICAL, 
MenuItem, listeners 
); 

JPanel panel = new JPanel(); 
panel.add(menu); 
panel.setSize(50, 100); 
f.add(panel, BorderLayout.WEST); 
f.setSize(500, 400); 
f.setLocationRelativeTo(null); 
     f.setVisible(true); 

    } 

} 

답변

3

대신의 ActionListener의 목록에 null의 추가, 리스너를 추가 (내 예에서, 같은 청취자,하지만 당신은 아이디어를 얻을) : 여기

내 코드입니다

List<ActionListener> listeners = new ArrayList<ActionListener()>; 
ActionListener listener = new ActionListener() { 
    @Override void actionPerformed(ActionEvent e) { 
     // do something 
    } 
}; 
for(int i=0; i<4; i++){ 
    listeners.add(listener); 
} 
관련 문제