java - 我正在尝试制作一个显示元素列表的 JFrame,每个元素代表目录中的一个文件

标签 java swing java.util.scanner defaultlistmodel

好吧,我正在制作一个从文本文件文件夹中提取用户信息的软件。每个文本文件有 2 行。名字/姓氏和电话号码。我正在编写打开 JFrame 的代码,该 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.setDefaultCloseOperation(),那是因为这是由主程序上按钮的 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

看看The try-with-resources Statement有关 try (Scanner s = new Scanner(file)) {

的更多详细信息

关于java - 我正在尝试制作一个显示元素列表的 JFrame,每个元素代表目录中的一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092148/

相关文章:

java - 解压JDK安装程序时没有 "src.zip"

java - JFrame 到 JApplet 在浏览器中不起作用

java - 无法让我的堆栈程序使用 pop

Java For循环只执行一次

java - 如何让 myfirstapp 在 android studio 上运行?

Java 窗口内容调整大小,但不超过最小大小

java - 用于控制台输入的 Java 扫描仪类的替代品

java - 为什么scanner.hasNextInt()方法需要一个字母来开始处理数字?

java - 使用 JNI 返回对象防止内存泄漏

java - 绘制组件未被调用