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

发布于 2024-11-29 16:19:22 字数 833 浏览 0 评论 0原文

我试图将文本文件的每一行复制到 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);            
    }
(...)

I'm trying to copy each line of a text file into a jcomboBox, but it displays only the first line of the text file in the jcomboBox...I don't understand why.
Can you please explain me what's wrong?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

极度宠爱 2024-12-06 16:19:22

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

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

注意:我假设数组 lines 足够大

That's because you read only the first line and close the file, consider:

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

Note: I assume the array lines is big enough

薆情海 2024-12-06 16:19:22

您只读取文件的第一行。所以 JCombobox 中不能有更多内容。您应该花一些时间阅读所有行,直到读完。

You're only reading the first line of the file. So there can't be more in the JCombobox. You should use a while and read all lines till you reach the end.

爱你是孤单的心事 2024-12-06 16:19:22

替换

read = in.readLine();

lines[w]=read;

while((read = in.readLine())!=null){
    lines[w++]=read;
}

replace

read = in.readLine();

lines[w]=read;

with

while((read = in.readLine())!=null){
    lines[w++]=read;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文