JFileChooser api

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

 

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

模式参数: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() 
          如果将文件选择器设置为允许选择多个文件,则返回选中文件的列表(文件[])。

getSelectedFile()
          返回选中的文件。

 

 

 
  1. package com.liang;

  2.  
  3. import java.awt.event.ActionEvent;

  4. import java.awt.event.ActionListener;

  5. import java.io.File;

  6.  
  7. import javax.swing.JButton;

  8. import javax.swing.JFileChooser;

  9. import javax.swing.JFrame;

  10. import javax.swing.JLabel;

  11.  
  12. public class FileChooser extends JFrame implements ActionListener{

  13. JButton open=null;

  14. public static void main(String[] args) {

  15. new FileChooser();

  16. }

  17. public FileChooser(){

  18. open=new JButton("open");

  19. this.add(open);

  20. this.setBounds(400, 200, 100, 100);

  21. this.setVisible(true);

  22. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  23. open.addActionListener(this);

  24. }

  25. @Override

  26. public void actionPerformed(ActionEvent e) {

  27. // TODO Auto-generated method stub

  28. JFileChooser jfc=new JFileChooser();

  29. jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );

  30. jfc.showDialog(new JLabel(), "选择");

  31. File file=jfc.getSelectedFile();

  32. if(file.isDirectory()){

  33. System.out.println("文件夹:"+file.getAbsolutePath());

  34. }else if(file.isFile()){

  35. System.out.println("文件:"+file.getAbsolutePath());

  36. }

  37. System.out.println(jfc.getSelectedFile().getName());

  38.  
  39. }

  40.  
  41. }


 

 

JFileChooser效果图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值