我正在尝试制作一个实用程序,用户应该只能选择“.pdf”文件,别无其他。这是我的代码:
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
// jfc.setFileFilter(filter);
jfc.addChoosableFileFilter(new FileNameExtensionFilter("*.pdf", "pdf"));
int returnValue = jfc.showOpenDialog(null);
// int returnValue = jfc.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println(selectedFile.getAbsolutePath());
}
但问题是文件选择器对话框还提供了“所有文件”选项,这破坏了工作。我希望用户只选择 .pdf 文件。如何做到这一点?