尝试将 txt 文件中的项目添加到 jList

发布于 2024-11-25 06:13:45 字数 904 浏览 0 评论 0原文

我有以下在单击按钮时执行的 try catch 块。

  try {
//picking up the file I want to read in
 BufferedReader in = new BufferedReader(new FileReader("C:\\users\\me\\desktop\\blah.txt"));
 String line;                                           
 try {
    //read through the file until there is nothing left and add each line to list
         while((line = in.readLine()) != null){  
            jList1.add(line, jList1);
                    }

               } catch (IOException ex) {
                    Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
           }
      } catch (FileNotFoundException ex) {
                Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
  }

我可以成功 System.out.println(line) 所以我知道有些东西工作正常。我无法使用文本文件中的行填充列表。上面的代码告诉我,我无法将容器父级添加到自身。

尝试查找更多信息只会让我更加困惑。我遇到过一些地方说 jLists 比这更复杂?

I have the following try catch block that executes on a button click.

  try {
//picking up the file I want to read in
 BufferedReader in = new BufferedReader(new FileReader("C:\\users\\me\\desktop\\blah.txt"));
 String line;                                           
 try {
    //read through the file until there is nothing left and add each line to list
         while((line = in.readLine()) != null){  
            jList1.add(line, jList1);
                    }

               } catch (IOException ex) {
                    Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
           }
      } catch (FileNotFoundException ex) {
                Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
  }

I can successfully System.out.println(line) so I know something is working right. I am unable to populate the list with the lines from the text file. The above code tells me I cannot add containers parent to self.

Attempting to find more information has only confused me more. I have come across some places that say jLists are more complciated than this?

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

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

发布评论

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

评论(2

明月松间行 2024-12-02 06:13:45

存在很多错误,太多了,无法一一评论:

1) 基本 I/O

2) 异常

3) <一href="http://download.oracle.com/javase/tutorial/uiswing/components/list.html" rel="nofollow">如何使用列表

4) 示例

    BufferedReader in = null;
    String line;
    DefaultListModel listModel = new DefaultListModel();
    try {
        in = new BufferedReader(new FileReader("C:\\users\\me\\desktop\\blah.txt"));
        while ((line = in.readLine()) != null) {
            listModel.addElement(line); //(String.valueof(line));
        }
    } catch (IOException ex) {
        Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (in != null) {
            in.close();
        }
    }
    JList jList1 = new JList(listModel);

There are many mistakes present, too many to comment on all of them:

1) Basic I/O

2) Exceptions

3) How to Use Lists

4) Examples

    BufferedReader in = null;
    String line;
    DefaultListModel listModel = new DefaultListModel();
    try {
        in = new BufferedReader(new FileReader("C:\\users\\me\\desktop\\blah.txt"));
        while ((line = in.readLine()) != null) {
            listModel.addElement(line); //(String.valueof(line));
        }
    } catch (IOException ex) {
        Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (in != null) {
            in.close();
        }
    }
    JList jList1 = new JList(listModel);
病女 2024-12-02 06:13:45

你确实做不到:
再次阅读这一行: jList1.add(line, jList1); 您真正的意思是什么?您正在将 jList1 添加到 jList1,对吧?检查代码并进行相应修复。

You really cannot do it:
Read again this line: jList1.add(line, jList1); What did you really mean? You are adding jList1 to jList1, right? Check the code and fix it accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文