JFileChooser的简单使用

JFileChooser其实就是控制弹出一个框供用户选择保存和打开文件用的。


JFileChooser() 
          构造一个指向用户默认目录的 JFileChooser
JFileChooser(File currentDirectory)
          使用给定的 File 作为路径来构造一个 JFileChooser


setFileSelectionMode(int mode)
          设置 JFileChooser,以允许用户只选择文件、只选择目录,或者可选择文件和目录。

mode参数:FILES_AND_DIRECTORIES   指示显示文件和目录。

      FILES_ONLY                             指示仅显示文件。

     DIRECTORIES_ONLY                指示仅显示目录。

showDialog(Component parent,String approveButtonText)
          弹出具有自定义 approve 按钮的自定义文件选择器对话框。

showOpenDialog(Component parent)
          弹出一个 "Open File" 文件选择器对话框。

showSaveDialog(Component parent)
          弹出一个 "Save File" 文件选择器对话框。

setMultiSelectionEnabled(boolean b)
          设置文件选择器,以允许选择多个文件。

getSelectedFiles() 
          如果将文件选择器设置为允许选择多个文件,则返回选中文件的列表(File[])。

getSelectedFile()

          返回选中的文件。

</pre><pre name="code" class="java">使用代码如下:
基本用法
JFileChooser jf = new JFileChooser();
jf.setDialogTitle("Open file");//设置对话框名称
int result = jf.showOpenDialog(this);  // 打开"打开文件"对话框
// int result = jf.showSaveDialog(this);  // 打"开保存文件"对话框
if (result == JFileChooser.APPROVE_OPTION) {//当选择文件时才可以运行,避免在对话框中直接点关闭的错误
File file = jf.getSelectedFile();//获取文件
}
<p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);"> <wbr></wbr></p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">
</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">多选情况</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">在基本用法中,设置</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">jf.setMultiSelectionEnabled<wbr>(true);</wbr></p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">即可实现文件的多选。</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">读取选择的文件时需使用</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">File[] files = jf.getSelectedFiles();</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);"> <wbr></wbr></p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">选择目录</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">利用这个打开对话框,不仅可以选择文件,还可以选择目录。</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">其实,对话框有一个FileSelectionMode属性,其默认值为“JFileChooser.FILES_ONLY”,只需要将其修改为“JFileChooser.DIRECTORIES_ONLY”即可。</p><p style="line-height: 28px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; background-color: rgb(204, 206, 208);">JFileChooser jf = new JFileChooser();
jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jf.setDialogTitle("Select path to save");
int result = jf.showOpenDialog(PrintDatetime.this);
if (result == JFileChooser.APPROVE_OPTION) {
String path = jf.getSelectedFile().getAbsolutePath());
...
}</p>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值