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());