我正在尝试为我的 JFileChooser 设置文件过滤器。这是我的代码:
JFileChooser picker= new JFileChooser();
picker.setFileFilter(new FileNameExtensionFilter("txt"));
int pickerResult = picker.showOpenDialog(getParent());
if (pickerResult == JFileChooser.APPROVE_OPTION){
System.out.println("This works!");
}
if (pickerResult == JFileChooser.CANCEL_OPTION){
System.exit(1);
}
当我运行我的程序时,文件选择器会出现,但它不会让我选择任何 .txt 文件。相反,它在控制台中这样说:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Extensions must be non-null and not empty
我该如何解决?