0

我在使用 Java 时遇到了麻烦JFileChooser,想知道是否有人可以帮助我。这可能是一件非常简单的事情,但我就是不知道哪里出了问题。

JFileChooser我单击我的导入按钮时,窗口打开正常,我可以导航到任何字段,但我无法将它们读入我的JTextFields.

这是我的JFileChooser方法:

public void importFile() {
    JFileChooser chooser = new JFileChooser();//A
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //a
        try {
            BufferedReader file_in = new BufferedReader(
            new FileReader(chooser.getSelectedFile().getPath()));
            int i = 0;

            String name = "",hnumber = "", mnumber = "", address = "";

            while (((fileLines = file_in.readLine()) != null)) {
                if (fileLines.length() > 0) {
                    i++;
                    if (i == 1) {
                        name = fileLines;
                    } else if (i == 2) {
                        hnumber = fileLines;
                    } else if (i == 3) {
                        mnumber = fileLines;
                    } else if (i == 4) {
                        address = fileLines;

                        String[] nameArray = name.split(" ");

                        Contact c = new Contact (nameArray[1], nameArray[0], 
                        hnumber, mnumber, address);
                        contactList.add(c);
                        index = 0;
                    }
                }
            }

            for (int j = 0; j < contactList.size(); j++) {
                System.out.print(contactList.get(j).getname());
                System.out.print(" ");
                System.out.println(contactList.get(j).getmnumber());
                System.out.println(contactList.get(j).gethnumber());
                System.out.println(contactList.get(j).getaddress());
                System.out.println(contactList.get(j).getsurname());
                System.out.println(" ");
            }

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
4

2 回答 2

0

您应该使用 List 或 StringBuilder 以便于获取行。结果你会得到任何错误吗?调试确实有助于查看您的程序在哪里中断。

这是我快速为您整理的内容:

public void importFile() {
    JFileChooser chooser = new JFileChooser();//A
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //a
        try {
            FileReader fr = new FileReader(chooser.getSelectedFile().getPath());
            BufferedReader file_in = new BufferedReader(fr);
            List lines = new List();
            String line = new String("");
            while ((line = file_in.readLine()) != null) {
                list.add(line);
                if (list.size() >= 3) {
                    String[] nameArray = ((String)list.get(0)).split(" ");
                    Contact c = new Contact (nameArray[1], nameArray[0], 
                            (String)list.get(1), (String)list.get(2), 
                            (String)list.get(3));
                    contactList.add(c);
                }
                System.out.println(list.get(list.size()-1)); // Debug
            }
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

我没有编译它,所以可能有一些错别字或类似...

于 2009-11-29T20:12:11.070 回答
0

它导入一个名为“contactList”的数组列表,您可以看到它位于底部的第 5 行。所以它不会直接进入 JTextFields 但无论哪种方式我都无法让它工作。

于 2009-11-29T20:14:37.050 回答