先看效果吧!

说明:选择文件或者文件夹。本例子就直接在控制台输出文件或者文件夹的路径。实际开发中,就可以将文件或文件夹的路径封装为File的实例来使用了。

package test;

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.JLabel; @SuppressWarnings("serial")
public class FileChooser extends JFrame implements ActionListener{
JButton open=null;
public static void main(String[] args) {
new FileChooser();
}
public FileChooser(){
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) {
// TODO Auto-generated method stub
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
if(file.isDirectory()){
System.out.println("文件夹:"+file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("文件:"+file.getAbsolutePath());
}
System.out.println(jfc.getSelectedFile().getName()); } }

最新文章

  1. 转载:《TypeScript 中文入门教程》 9、泛型
  2. ORM系列之三:Dapper
  3. JDBC小工具--TxQueryRunner及其单元测试
  4. spring mvc实现查询
  5. QTP10.0安装说明
  6. std::string和int类型的相互转换(C/C++)
  7. 获得树形json串
  8. windows下使用pthread
  9. 《孵化twitter》读书笔记
  10. cs231n spring 2017 lecture16 Adversarial Examples and Adversarial Training 听课笔记
  11. 数据库mysql大全(高级版)
  12. OOCSS(面向对象的CSS)总结
  13. Java java jdbc thin远程连接并操作Oracle数据库
  14. install scala & spark env
  15. IDC:2014年的十大 IT 趋势
  16. tcp 大文件上传 ,切换目录 及登陆文件加盐处理
  17. java程序运行时间
  18. centos如何设置定时任务
  19. Spring------mysql读写分离
  20. C# Oracle.ManagedDataAccess 批量更新表数据

热门文章

  1. dr03_
  2. 安装rackspace private cloud --5 Deployment configuration
  3. Java-集合类源码List篇(三)
  4. Office文件的实质是什么
  5. AIM Tech Round 4 (Div. 2)ABCD
  6. java maven、springmvc、mybatis 搭建简单Web项目学习笔记
  7. 27 python 网络基础之网络协议
  8. C程序设计语言阅读笔记
  9. webpack里CommonJS的require与ES6 的module.exports加载模块有何不同
  10. [转]200 OK (from cache) 与 304 Not Modified------没有这个规则(ETag是否移除)!!!from cache和304,请查看顶部的流程图!