JavaSwing文件选择器 JFileChooser的使用
2024-09-04 17:05:16
先看效果吧!
说明:选择文件或者文件夹。本例子就直接在控制台输出文件或者文件夹的路径。实际开发中,就可以将文件或文件夹的路径封装为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()); } }
最新文章
- 转载:《TypeScript 中文入门教程》 9、泛型
- ORM系列之三:Dapper
- JDBC小工具--TxQueryRunner及其单元测试
- spring mvc实现查询
- QTP10.0安装说明
- std::string和int类型的相互转换(C/C++)
- 获得树形json串
- windows下使用pthread
- 《孵化twitter》读书笔记
- cs231n spring 2017 lecture16 Adversarial Examples and Adversarial Training 听课笔记
- 数据库mysql大全(高级版)
- OOCSS(面向对象的CSS)总结
- Java java jdbc thin远程连接并操作Oracle数据库
- install scala &; spark env
- IDC:2014年的十大 IT 趋势
- tcp 大文件上传 ,切换目录 及登陆文件加盐处理
- java程序运行时间
- centos如何设置定时任务
- Spring------mysql读写分离
- C# Oracle.ManagedDataAccess 批量更新表数据
热门文章
- dr03_
- 安装rackspace private cloud --5 Deployment configuration
- Java-集合类源码List篇(三)
- Office文件的实质是什么
- AIM Tech Round 4 (Div. 2)ABCD
- java maven、springmvc、mybatis 搭建简单Web项目学习笔记
- 27 python 网络基础之网络协议
- C程序设计语言阅读笔记
- webpack里CommonJS的require与ES6 的module.exports加载模块有何不同
- [转]200 OK (from cache) 与 304 Not Modified------没有这个规则(ETag是否移除)!!!from cache和304,请查看顶部的流程图!