为什么JFileChooser不会关闭?

时间:2014-12-01 00:11:43

标签: java jfilechooser

下面的代码运行一个名为 displayClassSet()的方法。我把它放在do / while中重新创建错误,这是JFileChooser没有关闭。当do / while不存在时,它可以正常工作。在原始代码(超过700行)中,它通过使用开关获得此方法。因此,用户选择执行 displayClassSet()的选项。完成后,它将返回菜单。此时,JFileChooser保持打开状态,不允许任何新的菜单选择。我希望我足够清楚。我认为这比尝试在这里转储一个巨大的文件更好。在下面粘贴的示例代码中,如果您在返回后选择“n”,则会挂起。如果选择“y”,它将关闭,但同样,用户可能无法在原始代码中完成。他们不能因为我提到的悬挂问题。我还提供了打算打开的TXT文件样本。

我尝试过:bufRdr.close();in.close();。我还在JFileChooser上阅读了几个网站。

你能看出我做错了吗?

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.swing.JFileChooser;

public class TestingArrays {

    public static void main(String[] args) throws IOException {
        boolean done = false;
        Scanner console = new Scanner(System.in);
        do {
            displayClassSet();
            done = getYNConfirm(console, "Done?");
        } while (!done);

    }

    public static Boolean getYNConfirm(Scanner pipe, String prompt)
    {
        Boolean result = false;
        String input = "";
        String trash = "";

        do {
            System.out.print(prompt + " [y/n] ");
            if (pipe.hasNextDouble()) {
                trash = pipe.nextLine();
                System.out.println("Error: You entered \"" + trash + "\", Try again.");
            } else {
                input = pipe.nextLine();
                if (input.equalsIgnoreCase("y")) {
                    result = true;
                } else if (input.equalsIgnoreCase("n")) {
                    result = false;
                }
            }
        } while (!input.equalsIgnoreCase("y") && !input.equalsIgnoreCase("n"));
        return result;
    }

    static void displayClassSet() throws IOException
    {
        File classFile = new File("/");
        int row = 0;
        int col = 0;
        //int columnLength = 0;
        //Scanner inFile;
        int lineCount = 0;
        int wordCount = 0;
        int charCount = 0;
        int countColumns = 0;
        int maxLineLength = 0;
        int sum = 0;
        int high = 0;
        int low = 100;
        String line;
        Scanner in;

        JFileChooser chooser = new JFileChooser();

        try {
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                classFile = chooser.getSelectedFile();
                in = new Scanner(classFile);
                while (in.hasNextLine()) {
                    line = in.nextLine();
                    lineCount++; // Same as lineCount = lineCount + 1;
                    charCount += line.length();
                    wordCount += countWords(line);
                    countColumns += countColumns(line);
                    int columnOneWidth = 25;
                    int gradeColumnWidth = 10;
                    maxLineLength = columnOneWidth + (((countColumns / lineCount + 2)) * gradeColumnWidth);
                }

                String[][] data = new String[lineCount][countColumns / lineCount];

                BufferedReader bufRdr = new BufferedReader(new FileReader(classFile));

                //read each line of text file
                while ((line = bufRdr.readLine()) != null && row < lineCount) {
                    StringTokenizer st = new StringTokenizer(line, ",");
                    while (st.hasMoreTokens()) {
                        //get next token and store it in the array
                        data[row][col] = st.nextToken();
                        col++;
                    }
                    col = 0;
                    row++;
                }
                bufRdr.close();
                in.close();
                // CODE REMOVED - SIMPLE FOR LOOPS TO DISPLAY INFORMATION
                System.out.println("display stuff here...");
            } else {
                System.out.println("You must choose a file.\nReturn to the menu.");
                return;
            }
        } catch (FileNotFoundException ex) {
            System.out.println("Error: Could not open class file!");
            System.exit(0);
        } catch (IOException ex) {
            ex.getStackTrace();
            System.out.println("IO ERROR trying to read file!");
            return;
        }
    }

    static int countWords(String in) {
        String trim = in.trim();

        if (trim.isEmpty()) {
            return 0;
        }
        return trim.split("\\s+").length; //separate string around spaces
    }

    static int countColumns(String in) {
        String trim = in.trim();

        if (trim.isEmpty()) {
            return 0;
        }
        return trim.split(",").length; //separate string around spaces
    }
}

TEXT FILE CONTAINS:

2nd Grade Pyromancy, Lab01, Quiz01, Test01
Ferrel Quinn, 75, 70, 80
Billy Brago, 70, 80, 90
Sally Simpson, 50, 60, 60
Django Rude, 100, 90, 90

2 个答案:

答案 0 :(得分:0)

我创建了自己的表单,创建了一个按钮,并为该按钮添加了一个动作事件监听器。 我稍微修改了你帖子中的代码。有了这个,我就可以回到原来的形式,没有任何问题 这些是对displayClassSet()函数代码的唯一更改,我没有修改问题中提供的任何其他代码。

File classFile;
int row = 0;
int col = 0;
//int columnLength = 0;
//Scanner inFile;
int lineCount = 0;
int wordCount = 0;
int charCount = 0;
int countColumns = 0;
int maxLineLength = 0;
int sum = 0;
int high = 0;
int low = 100;
String line;
Scanner in;

File workingDirectory = new File(System.getProperty("user.dir"));//I only added this so as to 
//know the directory being used and would not have to search for it.
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(workingDirectory);

我将showOpenDialog和classFile声明移出了try / catch语句

if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
{
    classFile = chooser.getSelectedFile();
    try 
    {
        in = new Scanner(classFile);
        while (in.hasNextLine()) {
            line = in.nextLine();
            lineCount++; // Same as lineCount = lineCount + 1;
            charCount += line.length();
            wordCount += countWords(line);
            countColumns += countColumns(line);
            int columnOneWidth = 25;
            int gradeColumnWidth = 10;
            maxLineLength = columnOneWidth + (((countColumns / lineCount + 2)) * gradeColumnWidth);
        }
    }

我为每个不同的文件打开用法

分隔了try / catch语句
    catch (FileNotFoundException ex) 
    {
        System.out.println("Error: Could not open class file!");
        System.exit(0);
    }             
    catch (IOException ex) 
    {
        ex.getStackTrace();
        System.out.println("IO ERROR trying to read file!");
       //return;
    }

    String[][] data = new String[lineCount][countColumns / lineCount];
    try
    {
        BufferedReader bufRdr = new BufferedReader(new FileReader(classFile));

        //read each line of text file
        while ((line = bufRdr.readLine()) != null && row < lineCount) 
        {
            StringTokenizer st = new StringTokenizer(line, ",");
            while (st.hasMoreTokens()) {
                //get next token and store it in the array
                data[row][col] = st.nextToken();
                col++;
            }
                col = 0;
                row++;
        }
    }catch (FileNotFoundException ex) 
    {
        System.out.println("Error: Could not open class file!");
        System.exit(0);
    }             
    catch (IOException ex) 
    {
        ex.getStackTrace();
        System.out.println("IO ERROR trying to read file!");
        //return;
    }
    for(int x = 0; x < lineCount; x++)//I added this to check the contents of data[][]
    {
        for(int y = 0; y < (countColumns / lineCount); y++)
        {
            System.out.print(data[x][y]);
        }
        System.out.println("");
    }
} 
else 
{
    System.out.println("You must choose a file.\nReturn to the menu.");
    //return;
}

我希望这有助于解决您的问题。

答案 1 :(得分:0)

我实施了David Coler的所有建议(非常感谢您的时间!)。我仍然有我的特殊问题。我开始评论绝对一切,当发生的唯一事情是打开文件时,问题仍然存在。长话短说,我在PC上运行它,它在各方面都做得很好。我仍然认为我可以简化我的代码,但在Mac上似乎存在JFileChooser的问题。