Java 文件选择器循环

Java Filechooser Looping

我正在开发一个打开 JFileChooser 并允许用户选择包含姓名、身高和体重等信息的 .txt 文件的程序。

程序然后计算文件中列出的每个人的 BMI,并将它们显示在 JOptionPane 中。然后他们可以决定是选择另一个文件还是取消程序。

我已经为计算和显示计算出了一切,但我遇到的困难在于 JOptionPane 选择。我想在用户选择 "no" 选项或取消选择文件时显示一条消息,但我似乎无法这样做。我也不确定如何在他们选择 "yes" 选项时重新打开 JFileChooser。如果有人可以就如何实现这一目标甚至我可能做错了什么提供一些指导,我将不胜感激。先感谢您!到目前为止,这是我的代码:

public static void main(String[] args) throws FileNotFoundException, IOException {
    readFile();
}

public static int readFile() throws FileNotFoundException, IOException {
    int choice = (JOptionPane.YES_OPTION);

    while (choice == (JOptionPane.YES_OPTION)) {
        File file;
        JFileChooser fileChooser;

        fileChooser = new JFileChooser();
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setSize(300, 300);
        int returnVal = fileChooser.showOpenDialog(null);
        while (returnVal == JFileChooser.APPROVE_OPTION) {

            file = fileChooser.getSelectedFile();

            String report = BMIRecord.report(file);

            int dialogResult = JOptionPane.showConfirmDialog(null, report + "\n\nWould you like to try another file?", "BMI Calculations", JOptionPane.YES_NO_OPTION);
            return choice;
        }

    }

    while (choice == (JOptionPane.NO_OPTION)) {
        JOptionPane.showMessageDialog(null, "Session ended.");
        return choice;
    }
    return 0;
}

所有的厕所ps都是混淆的问题。您只需要一个循环,即提示用户输入新文件并提示用户继续的方式...

基本上,你需要提示一个文件,如果 JFileChooser returns 与 JFileChooser.APPROVE_OPTION,你计算 BMI 并显示报告,然后可以提示用户输入另一个文件。否则,您可能会认为他们选择不继续。

你一直这样做,直到 JOptionPane.showConfirmDialog 等于 JOptionPane.NO_OPTION(或者用户取消 JFileChooser,这可能是同一件事)

类似...

public static int readFile() throws FileNotFoundException, IOException {

    JFileChooser fileChooser = new JFileChooser();
    int choice = JOptionPane.NO_OPTION;
    do {
        choice = JOptionPane.NO_OPTION;
        int returnVal = fileChooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            String report = BMIRecord.report(file);
            choice = JOptionPane.showConfirmDialog(null, report + "\n\nWould you like to try another file?", "BMI Calculations", JOptionPane.YES_NO_OPTION);
        }

    } while (choice == JOptionPane.YES_OPTION);

    return 0;
}

例如...(ps,我不确定"suppose"到return的方法是什么,所以我只是returned 0)

看起来你的两个 while 循环,在第一个 while 循环之后,应该是 if 语句。另外,不要return选择。相反,让它从 JOptionPane.showConfirmDialog().

接收结果
public static int readFile() throws FileNotFoundException, IOException
{
    int choice=(JOptionPane.YES_OPTION);
    while(choice == (JOptionPane.YES_OPTION))
    {
        File file;
        JFileChooser fileChooser;

        fileChooser = new JFileChooser();
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setSize(300, 300);
        int returnVal = fileChooser.showOpenDialog(null);

        // If statement, instead of while
        String report = "";
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            file = fileChooser.getSelectedFile();

            report = BMIRecord.report(file);
        }

        // Ask for another file regardless of the result of the fileChooser
        choice = JOptionPane.showConfirmDialog(null, report + "\n\nWould you like to try another file?","BMI Calculations",JOptionPane.YES_NO_OPTION); 
    }

    // If statement, instead of while.  Also, this if isn't really needed.  Once the while loop exits, the NO_OPTION is implied to have been selected.    
    if (choice == (JOptionPane.NO_OPTION))
    {
        JOptionPane.showMessageDialog(null, "Session ended.");
    }
    return 0;
}