java選擇文件
① java中jfilechooser有沒有可以選擇文件夾的
private JFileChooser fc=new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能選擇目錄
String path=null;
File f=null;
try{
flag=fc.showOpenDialog(null);
}
catch(HeadlessException head){
System.out.println("Open File Dialog ERROR!");
}
if(flag==JFileChooser.APPROVE_OPTION){
//獲得該文件
f=fc.getSelectedFile();
path=f.getPath();
}
//以上獲得選擇的文件夾
//若要判斷其中是否還有其他目錄,可以這樣做
boolean hasSubDir=false;
File dir=new File(path);
//獲得改目錄下的文件的文件名,如果沒有的話,filesName.length()=0
String filesName=dir.list();
for(int i=0;i<filesName.length();i++){
File temp=new File(path+"/"+filesName[i]);
if(temp.isDirectory()){
hasSubDir=true;
break;
}
}
② Java如何做出好看的本地文件選擇器
更改一下外觀為windows的:
if(UIManager.getLookAndFeel().isSupportedLookAndFeel()){
final String platform = UIManager.getSystemLookAndFeelClassName();
// If the current Look & Feel does not match the platform Look & Feel,
// change it so it does.
if (!UIManager.getLookAndFeel().getName().equals(platform)) {
try {
UIManager.setLookAndFeel(platform);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
如果不需要文件過濾可以選擇 FileDialog來打開仔衡文件,這個是調用操作系統的文件筐野肆打開文件的,但是在window下實現不了文件過濾,其他系統下可以。如果念脊做用JFileChoose可以很容易實現,文件過濾,但是界面默認是java外觀。如果要好看,可以設置一下顯示外觀。