Java_java中JFileChooser类(java_swing提供的文件选择对话框)
2016-12-30 19:06
309 查看
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()
返回选中的文件。
代码:
package com.liang;
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;
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());
}
}
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()
返回选中的文件。
代码:
package com.liang;
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;
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());
}
}
JFileChooser 效果图如下:
相关文章推荐
- JFileChooser (Java Swing提供的文件选择对话框)
- 设置文件选择对话框大小及纵向大小不可变--JAVA-SWING-JFileChooser-setMinimumSize
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
- 设置Java Swing的文件选择对话框只选择目录的方法
- java swing中JFileChooser保存文件时,在文件选择窗口提示是否覆盖
- java之GUI文件对话框javax.swing.JFileChooser
- Java文件选择对话框JFileChooser使用详解
- Java-记事本程序、文件选择对话框(JFileChooser)
- java中文件选择对话框
- java swing 文件选择,…
- JAVA文件选择JFileChooser使用例子
- Swing学习_文件选择框(JFileChooser)框示例代码
- JFileChooser 文件选择对话框API
- java swing 文件选择,设置默认文件选择路径,桌面路径
- Java 使用对话框选择文件并输出到控制台
- Java按位置解析文本文件(使用Swing选择文件)
- java文件选择对话框,文件名不可编辑
- java选择文件时提供图像缩略图[转]
- java swing 文件选择,设置默认文件选择路径,桌面路径
- Java——文件选择框:JFileChooser