我在使用 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();
}
}
}