0

我之前问过类似的问题,但意识到我无法解决的主要问题:

当前有一个名为SundayList的 ArrayList ,一旦加载框架AddStudent就会加载(GUI 位)

添加学生课程: 已编辑

public class AddStudent extends javax.swing.JFrame {

    public AddStudent() {
    initComponents();
     }
private void loadLists() throws IOException
    {
        //Creating the array of Activities to put into the ComboBoxes
        File f = new File("Activities.dat");

        sundayList = new ArrayList<>();
        mondayList= new ArrayList<>();
        tuesdayList= new ArrayList<>();
        wednesdayList= new ArrayList<>();
        thursdayList= new ArrayList<>();


try{
    BufferedReader reader = new BufferedReader(new FileReader(f));

     while(reader.ready())
        {
            String CDay = reader.readLine();                               
            String CActivityName = reader.readLine();
            String CSupervisor = reader.readLine();
            String CLocation = reader.readLine();
            String CPaid = reader.readLine();
            String nothing = reader.readLine();

            if(CDay.equals("Sunday"))
            {
                sundayList.add(CActivityName);
            }
            else if(CDay.equals("Monday"))
            {
                mondayList.add(CActivityName);
            }
            else if(CDay.equals("Tuesday"))
            {
                tuesdayList.add(CActivityName);
            }
            else if(CDay.equals("Wednesday"))
            {
                wednesdayList.add(CActivityName);
            }
            else if(CDay.equals("Thursday"))
            {
                thursdayList.add(CActivityName);
            }                
    }
    reader.close();
}
catch (IOException ex) 
{
    Logger.getLogger(StartUpFrame.class.getName()).log(Level.SEVERE, null, ex);
} 
}
...
comboboxSunday = new javax.swing.JComboBox();
...
}



 public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddStudent().setVisible(true);
            }
        });
    }

首先,我尝试将SundayList列表调用到组合框comboboxSunday中来填充它,但只得到了找不到符号错误。

我需要做什么才能使这成为可能?

另外,我计划避免使用我以前见过的涉及 mySQL 的方法,因为我不熟悉它。

组合框的当前编码

Netbeans 为组合框自动生成的代码是:

comboboxSunday = new javax.swing.JComboBox();

comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel<>(sundayList.toArray(new String[sundayList.size()])));
4

1 回答 1

3

该变量SundayList仅限于构造函数的范围。假设您在方法中创建您JComboBoxinitComponents,您将无法访问此变量。

但是,您可以创建SundayList一个类成员变量,允许您在方法中使用变量。最好有一种方法来加载数据,而不是在 UI 构造函数中使用非 UI 功能:

public class AddStudent {
   private List<String> sundayList;
   private List<String> mondayList;
   ...

   private void loadLists() throws IOException {
      sundayList = new ArrayList<>();
      ...

然后添加:

comboboxSunday.setModel(new DefaultComboBoxModel<>(sundayList.toArray(new String[sundayList.size()])));

不要忘记调用新的加载方法:

AddStudent addStudent = new AddStudent();
addStudent.loadLists();
addStudent.setVisible(true);

另外:请注意,Java 命名约定表明该变量以小写字母开头,这将使SundayList sundayList.

于 2013-03-11T18:37:00.807 回答