1.目录选择框
JFileChooser chooser=new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result=chooser.showOpenDialog(null);
String fname=chooser.getName(chooser.getSelectedFile());
System.out.println("fname--->"+fname);
if(result==JFileChooser.APPROVE_OPTION)
{
String filePath=chooser.getSelectedFile().getPath();
System.out.println("filePath--->"+filePath);
if(filePath.endsWith(".xml"))
{
System.out.println("filePath--->"+filePath);
}
}
Object[] options = { "OK", "CANCEL" };
int results= JOptionPane.showOptionDialog(null, "是否保存?", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null, options, options[0]);
if(results==JOptionPane.OK_OPTION)
{
doSave();
}
if(results==JOptionPane.NO_OPTION )
{
doReturn();
}
2.文件选择框
javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
chooser.showOpenDialog(null);
String fname=chooser.getName(chooser.getSelectedFile());
本文介绍了如何使用Java Swing中的JFileChooser组件实现文件和目录的选择功能。通过设置选择模式为仅目录或文件,并获取所选路径,实现了基本的文件与目录选择流程。此外,还展示了如何通过对话框提示用户进行保存确认。

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



