我现在有一个array,n多个JButton
那我想给他们加上功能总不能一个一个加吧!放在for循环里面貌似还是不行啊!!!
我的代码如下!- public class StudentList implements ActionListener{
- private static JButton[] remove;
- public void actionPerformed(ActionEvent e){
- String numOfStudents = DataManager.getNumberOfStudents(EditClass.getSelectedTableName());
- try{
- studentNum=Integer.parseInt(numOfStudents);
- }
- catch(NumberFormatException ex){
- JOptionPane.showMessageDialog(frame, "Should be an integer: "+ex);
- }
- remove = new JButton[studentNum];
- for(int i=0;i<studentNum;i++){
- remove[i].setBounds(243,92+30*i,85,20);
- panel.add(remove[i]);
- remove[i].addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ae){
- try{
- int result = JOptionPane.showConfirmDialog(null,
- "Delete student?");
- switch (result) {
- case JOptionPane.YES_OPTION:
- DataManager.deleteStudent(Integer.parseInt(studentID[i]),studentName[i]);
- break;
- case JOptionPane.NO_OPTION:
- break;
- case JOptionPane.CLOSED_OPTION:
- break;
- }
- }catch (Exception ex){
- JOptionPane.showMessageDialog(panel, ex);
- }
- }
- });
- }
复制代码 Eclipse说问题出在DataManager.deleteStudent(Integer.parseInt(studentID),studentName);
这一行!
Cannot refer to a non-final variable i inside an inner class defined in a different method
这是怎么回事
我本来想用
public void actionPerformed(ActionEvent ae)
ae.getSource()这个方法不过这是array啊.....我应该咋办。。。。!
补充一下!Datamanager是另一个class
一定要帮忙啊谢谢大家!
|