JFileChooser :
//选择目录
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showOpenDialog(null);
String fname = chooser.getName(chooser.getSelectedFile());
if (result == JFileChooser.APPROVE_OPTION) {
String filePath = chooser.getSelectedFile().getPath();
System.out.println(filePath);
}
//保存文件:
JFileChooser fd = new JFileChooser();
HtmlFileFilter hff = new HtmlFileFilter();
fd.addChoosableFileFilter(hff);
fd.showSaveDialog(null);
File f = fd.getSelectedFile();
if (f != null){
System.out.println(f.getAbsolutePath() + ".html");
jTextField2.setText(f.getAbsolutePath() + ".html");
}
过滤器:
class HtmlFileFilter extends FileFilter {
public String getDescription() {
return "*.html(网页文件)";
}
public boolean accept(File file) {
String name = file.getName();
return name.toLowerCase().endsWith(".html");
}
}
本文介绍了如何使用Java中的JFileChooser组件来实现选择目录及保存文件的功能,并详细展示了如何通过自定义过滤器来限制文件类型。
7061

被折叠的 条评论
为什么被折叠?



