好的,所以我正在制作一个从文本文件文件夹中提取用户信息的软件。每个文本文件有2行。名/姓和电话号码。我编写的代码打开一个JFrame,显示文件夹中所有用户的列表。我写了一些代码,并尝试排除故障,但我不知道为什么它不工作。我认为这是一个GUI的东西,但是我看了那么多java文档的例子,我不知道我做错了什么。
我的代码在这个方法中:
public void createDir() {
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(600,400);
f.setLocationRelativeTo(null);
f.setTitle("User Directory");
DefaultListModel listModel = new DefaultListModel();
JList list = new JList(listModel);
File directory = new File("Z:\Documents\users");
File[] listOfUsers = directory.listFiles(); // returns a file array of all txt files in the user folder
String n = null;
String p = null;
Scanner s;
for (File file : listOfUsers) // runs for each file in the listOfUsers file array, replacing 'file' with the current userfile
{
if (file.isFile()) // if the item selected is a file
{
try
{
s = new Scanner(file); // creates a scanner to read the current user file
int i = 0; // sets the counter that is used to read name and phone number associated with the file
while(s.hasNextLine()) // reads the file using the counter to obtain the name and phone number, storing each in the 'n' and 'p' variables
{
if(i==0)
n = s.nextLine();
if(i==1)
p = s.nextLine();
i++;
}
listModel.addElement(n + " " + p); // the user shows up on the list item as "First Last ###-###-####"
s.close(); // closes the scanner for re-use
}
catch (FileNotFoundException e) // incase of exception thrown
{
System.out.print("exception occured: " + e);
}
}
}
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setVisibleRowCount(5);
list.setSelectedIndex(0);
JScrollPane listScrollPane = new JScrollPane(list); // creates a scrollpane for the JList component 'list'
JPanel dirPanel = new JPanel();
f.add(dirPanel);
dirPanel.add(listScrollPane, BorderLayout.CENTER); // adds the component
}
无论何时调用该方法,JFrame都会出现,但是整个窗口都是黑色的,除了角落里的一小条白色条。在那个时候,我无法关闭它,它被冻结了。如果你认为它没有关闭是因为f.t setdefaultcloseoperation()没有被调用,那是因为这是一个被MAIN程序上按钮的actionListener调用的功能。我没有得到错误信息,我不知道我应该做什么。整个程序冻结并无法关闭。
测试完你的代码,你有一个潜在的无限循环。
while(s.hasNextLine()) // reads the file using the counter to obtain the name and phone number, storing each in the 'n' and 'p' variables
{
if(i==0)
n = s.nextLine();
if(i==1)
p = s.nextLine();
i++;
}
基本上,在这里,如果文件实际上有两行以上,循环将永远不会退出,因为你基本上会忽略任何其他行
所以,相反,要么你需要忽略第二行之后的任何内容,把break
排除在循环之外,要么你需要抛出Exception
,这样你就知道哪些文件会给你带来问题…
try (Scanner s = new Scanner(file)) {
int i = 0; // sets the counter that is used to read name and phone number associated with the file
try (Scanner s = new Scanner(file)) {
int i = 0; // sets the counter that is used to read name and phone number associated with the file
while (s.hasNextLine()) // reads the file using the counter to obtain the name and phone number, storing each in the 'n' and 'p' variables
{
switch (i) {
case 0:
n = s.nextLine();
break;
case 1:
p = s.nextLine();
break;
default:
throw new IOException("Invalid file format, more then two lines have been found!");
}
i++;
System.out.println(i);
}
listModel.addElement(n + " " + p); // the user shows up on the list item as "First Last ###-###-####"
} catch (IOException e) // incase of exception thrown
{
System.out.print("exception occured: " + e);
e.printStackTrace();
}
同样,正如已经注意到的,您应该在创建UI之后最后调用setVisible
。
查看try-with-resources语句,了解有关try (Scanner s = new Scanner(file)) {
行
在添加组件到JFrame后放置f.setVisible(true)。即在最后一行,而不是第二行