package cn.deos.body;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileSystemView;
@SuppressWarnings("serial")
public class bbbbbbbbbb extends JFrame implements ActionListener{
JButton open=null;
public static void main(String[] args) {
new bbbbbbbbbb();
}
public bbbbbbbbbb(){
open=new JButton("open");
this.add(open);
this.setBounds(400, 200, 100, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
open.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
//文件选择器获取文件或者文件夹
//========================================
JFileChooser jfc=new JFileChooser();
//设置当前路径为桌面路径,否则将我的文档作为默认路径
FileSystemView fsv = FileSystemView .getFileSystemView();
jfc.setCurrentDirectory(fsv.getHomeDirectory());
//JFileChooser.FILES_AND_DIRECTORIES 选择路径和文件
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
//用户选择的路径或文件
if (jfc.showOpenDialog(bbbbbbbbbb.this)==JFileChooser.APPROVE_OPTION){
File file=jfc.getSelectedFile();
if(file.isDirectory()){
System.out.println("文件夹:"+file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("文件:"+file.getAbsolutePath());
}
}
//=======================================================
//文件选择器获取文件,这里只能获取文件,不能获取文件夹
/* JFileChooser jfc=new JFileChooser("C:\\");//可以直接在这设置默认路径
if(jfc.showOpenDialog(bbbbbbbbbb.this)==JFileChooser.APPROVE_OPTION){
File file=jfc.getSelectedFile();
System.out.println("文件:"+file.getAbsolutePath());
}*/
//====================================================
}
}