2012-04-29 18 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

而不是增加null年代到ActionListener的列表中,添加监听器(在我的例子,同样的监听器,但你的想法):

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); 
}