1

我的学生注册系统遇到了问题。我正在尝试通过从 a 中ArrayList选择一个对象来从 a中删除它JComboBox

public class Course {

 public List<Student> Students;
 public List<Module> Modules;

    public Course()
    {
         Students = new ArrayList<Student>();
         Modules = new ArrayList<Module>();
    }

public class Del_Student extends JFrame
{

  private Course newCourse;
    public Del_Student(Course aCourse)
    {
        newCourse = aCourse;
        JButton btnDel = new JButton("Delete");
        JButton btnCancel = new JButton("Cancel");
        JComboBox studentsBox = new JComboBox();
        studentsBox.setPreferredSize(new Dimension(185,25));

    for(int i=0; i<newCourse.Students.size();i++ )
    {
        String p = newCourse.Students.get(i).getFirstName();
        studentsBox.addItem(p);
    }

      btnDel.addActionListener
    (
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                newCourse.Students.remove(studentsBox.getSelectedItem());
            }
        }
    );

我已经从对象中添加了一个字符串以显示在 上JComboBox,所以我要做的是选择该项目(由学生姓名显示),然后删除所选项目。

4

1 回答 1

2

为了帮助其他有类似问题的人,我通过执行以下操作解决了该解决方案:

    btnDel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < newCourse.Students.size(); i++) {
                if (newCourse.Students.get(i).getFirstName().equals(studentsBox.getSelectedItem()
                        .toString())) {
                    newCourse.Students.remove(i);
                    JOptionPane.showMessageDialog(null, "Student Deleted");
                    studentsBox.removeAllItems();
                    for (int t = 0; t < newCourse.Students.size(); t++) {
                        String p = newCourse.Students.get(t).getFirstName();
                        studentsBox.addItem(p);
                    }
                }
            }
        }
    });
于 2013-03-13T10:52:27.670 回答