源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Java Swing组件文件选择器JFileChooser简单用法示例

  • 时间:2022-01-28 00:48 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Java Swing组件文件选择器JFileChooser简单用法示例
本文实例讲述了Java Swing组件文件选择器JFileChooser简单用法。分享给大家供大家参考,具体如下: 先来看效果: [img]http://files.jb51.net/file_images/article/201711/20171113113011765.png?20171013113113[/img] 说明:选择文件或者文件夹。本例子就直接在控制台输出文件或者文件夹的路径。实际开发中,就可以将文件或文件夹的路径封装为File的实例来使用了。 具体代码如下:
package awtDemo;
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());
  }
}
更多关于java相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/632.htm]Java数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/947.htm]Java字符与字符串操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/830.htm]Java操作DOM节点技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/687.htm]Java文件与目录操作技巧汇总[/url]》和《[url=http://www.1sucai.cn/Special/682.htm]Java缓存操作技巧汇总[/url]》 希望本文所述对大家java程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部