我正在尝试创建一个JFrame,它显示每个表示目录中文件的元素列表

时间:2015-10-13 00:10:27

标签: java swing java.util.scanner defaultlistmodel

好的,我正在制作一个从文本文件夹中提取用户信息的软件。每个文本文件有2行。姓/名和电话号码。我正在制作代码,打开一个JFrame,显示文件夹中所有用户的列表。我写了一些代码并尝试对其进行故障排除,但我不知道为什么它不起作用。我认为这是一个图形用户界面的东西,但我查看了很多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调用主程序。我没有收到任何错误消息,我不知道该怎么办。整个程序冻结并变得无法关闭。

2 个答案:

答案 0 :(得分:1)

测试过您的代码后,您将面临无限循环。

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 (Scanner s = new Scanner(file)) {

的详细信息,请查看The try-with-resources Statement

答案 1 :(得分:0)

在向JFrame添加组件后添加f.setVisible(true)。即在最后一行而不是第二行