0

我有一个应用程序应该允许用户从 JFileChooser 窗口中选择多个文件并处理它们。当我上周运行这个应用程序时,JFileChooser 允许用户像往常一样选择多个文件(按住 ctrl 或苹果键并选择文件)。但是,我今天尝试重新运行同一个应用程序,而 JFileChooser 不允许我以与以前相同的方式选择多个文件。我有我的 multiselectionenabled(true) 语句,并且 FileChooser 以前工作过,所以我很不确定发生了什么。这是代码:

    JFileChooser fc = new JFileChooser();
    System.out.println("Choose the files you would like to process.");
    fc.setMultiSelectionEnabled(true);
    fc.showOpenDialog(null);
    if(fc.getApproveButtonMnemonic()==JFileChooser.APPROVE_OPTION){
        files = fc.getSelectedFiles();
        assert(files.length!=0);
    }else{
        System.out.println("You've opted to cancel. System will now exit.");
    }

    for(int i=0; i<files.length; i++){
            System.out.println("Inside for loop.");
            System.out.println("Chosen File: "+files[i].getAbsolutePath());
    }
  return files;  
}

这很简单,而且以前一直有效,所以我不知道为什么今天它不起作用。有人可以帮我解决这个问题吗?这一步在我的程序中至关重要。

4

1 回答 1

1

在这个块:

fc.showOpenDialog(null);
if(fc.getApproveButtonMnemonic()==JFileChooser.APPROVE_OPTION){
    files = fc.getSelectedFiles();
    assert(files.length!=0);
}

您正在将 Approve 按钮助记符(可能是null)与整数常量 ( JFileChooser.APPROVE_OPTION) 进行比较。它应该是:

//fc.showOpenDialog(null);
if(fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
    files = fc.getSelectedFiles();
    assert(files.length!=0);
}
于 2013-08-12T14:48:34.187 回答