如何将文本文件中的行复制到JComboBox中?

0 java swing filereader jcombobox

我正在尝试将文本文件的每一行复制到jcomboBox中,但它只显示jcomboBox中文本文件的第一行...我不明白为什么.你能解释一下我有什么问题吗?

(...)
BufferedReader in;
    String read;

        try {
            in = new BufferedReader(new FileReader("D:/File.txt"));


            read = in.readLine();

            lines[w]=read;

             ++w;

            in.close();
        }catch(IOException e){
            System.out.println("There was a problem:" + e);
        }

    combo1 = new JComboBox(lines);

    combo1.setPreferredSize(new Dimension(100,20));
    combo1.setForeground(Color.blue);


    JPanel top = new JPanel();
    top.add(label);
    top.add(combo1);

    combo1.addActionListener(new ActionFichiers());

    container.add(top, BorderLayout.NORTH);
    this.setContentPane(container);
    this.setVisible(true);            
    }
(...)
Run Code Online (Sandbox Code Playgroud)

MBy*_*ByD 5

那是因为您只读取第一行并关闭文件,请考虑:

     try {
        in = new BufferedReader(new FileReader("D:/File.txt"));
        while((read = in.readLine()) != null){
            lines[w]=read;
           ++w;
        }
        in.close();
    }catch(IOException e){
        System.out.println("There was a problem:" + e);
    }
Run Code Online (Sandbox Code Playgroud)

注意:我认为数组lines足够大