您的位置:首页 > 其它

JFileChooser的简单使用

2016-07-01 12:14 211 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: