• P36 JFileChooser文件选择器


    系统:Win10
    Java:1.8.0_333
    IDEA:2020.3.4
    Gitee:https://gitee.com/lijinjiang01/JavaSwing

    1.概述

    JFileChooser:文件选取器。JFileChooser为用户选择文件提供了一种简单的机制,包括 打开文件 和 保存文件。
    JFileChooser 常用构造方法:

    /**
     * 参数说明:
     *     currentDirectory: 打开文件选取器时默认显示的文件夹(默认为用户文件夹)
     *     currentDirectoryPath: 打开文件选取器时默认显示的文件夹(默认为用户文件夹)
     */
    JFileChooser()
    
    JFileChooser(File currentDirectory)
    
    JFileChooser(String currentDirectoryPath)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    JFileChooser 常用方法:

    // 设置默认显示的文件夹
    void setCurrentDirectory(File dir)
    
    // 设置文件选择模式, 可选值如下:
    //     JFileChooser.FILES_ONLY: 只能选文件
    //     JFileChooser.DIRECTORIES_ONLY: 只能选文件夹
    //     JFileChooser.FILES_AND_DIRECTORIES: 文件和文件夹都可以选
    void setFileSelectionMode(int mode)
    
    // 设置是否允许同时选择多个(默认为不允许)
    void setMultiSelectionEnabled(boolean b)
    
    // 添加可供用户选择的文件过滤器
    void addChoosableFileFilter(FileFilter filter)
    
    // 设置默认使用的文件过滤器
    void setFileFilter(FileFilter filter)
    
    // 设置默认被选中的文件
    void setSelectedFile(File file)
    void setSelectedFiles(File[] selectedFiles)
    
    /*
     * 显示 打开文件 或 保存文件 的对话框(线程将被阻塞, 直到选择框被关闭)。
     *
     * 参数: 
     *     parent: 文件选取器对话框的父组件, 对话框将会尽量显示在靠近 parent 的中心; 如果传 null, 则显示在屏幕中心。
     * 
     * 返回值:
     *     JFileChooser.CANCEL_OPTION: 点击了取消或关闭
     *     JFileChooser.APPROVE_OPTION: 点击了确认或保存
     *     JFileChooser.ERROR_OPTION: 出现错误
     */
    int showOpenDialog(Component parent)
    int showSaveDialog(Component parent)
    
    // 获取选择的文件(一般在用户选择完文件点击了确认或保存后通过该方法获取选中的文件)
    File getSelectedFile()
    File[] getSelectedFiles()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    2.代码实例

    import com.lijinjiang.beautyeye.BeautyEyeLNFHelper;
    import javax.swing.*;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    public class Demo01 {
        public static void main(String[] args) {
            try {
                BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
                BeautyEyeLNFHelper.launchBeautyEyeLNF();
            } catch (Exception e) {
                e.printStackTrace();
            }
            final JFrame frame = new JFrame("Demo01");
            frame.setSize(400, 250);
    
            JPanel panel = new JPanel();
    
            // 创建文本区域, 用于显示相关信息
            final JTextArea msgTextArea = new JTextArea(10, 30);
            msgTextArea.setLineWrap(true);
            panel.add(msgTextArea);
    
            JButton openBtn = new JButton("打开");
            openBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showFileOpenDialog(frame, msgTextArea);
                }
            });
            panel.add(openBtn);
    
            JButton saveBtn = new JButton("保存");
            saveBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showFileSaveDialog(frame, msgTextArea);
                }
            });
            panel.add(saveBtn);
    
            // 设置窗口内容面板并显示
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        /*
         * 打开文件
         */
        private static void showFileOpenDialog(Component parent, JTextArea msgTextArea) {
            // 创建一个默认的文件选取器
            JFileChooser fileChooser = new JFileChooser();
    
            // 设置默认显示的文件夹为当前文件夹
            fileChooser.setCurrentDirectory(new File("."));
    
            // 设置文件选择的模式(只选文件、只选文件夹、文件和文件均可选)
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            // 设置是否允许多选
            fileChooser.setMultiSelectionEnabled(true);
    
            // 添加可用的文件过滤器(FileNameExtensionFilter 的第一个参数是描述, 后面是需要过滤的文件扩展名 可变参数)
            fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("zip(*.zip, *.rar)", "zip", "rar"));
            // 设置默认使用的文件过滤器
            fileChooser.setFileFilter(new FileNameExtensionFilter("image(*.jpg, *.png, *.gif)", "jpg", "png", "gif"));
    
            // 打开文件选择框(线程将被阻塞, 直到选择框被关闭)
            int result = fileChooser.showOpenDialog(parent);
    
            if (result == JFileChooser.APPROVE_OPTION) {
                // 如果点击了"确定", 则获取选择的文件路径
                File file = fileChooser.getSelectedFile();
    
                // 如果允许选择多个文件, 则通过下面方法获取选择的所有文件
                // File[] files = fileChooser.getSelectedFiles();
    
                msgTextArea.append("打开文件: " + file.getAbsolutePath() + "\n\n");
            }
        }
    
        /*
         * 选择文件保存路径
         */
        private static void showFileSaveDialog(Component parent, JTextArea msgTextArea) {
            // 创建一个默认的文件选取器
            JFileChooser fileChooser = new JFileChooser();
    
            // 设置打开文件选择框后默认输入的文件名
            fileChooser.setSelectedFile(new File("测试文件.zip"));
    
            // 打开文件选择框(线程将被阻塞, 直到选择框被关闭)
            int result = fileChooser.showSaveDialog(parent);
    
            if (result == JFileChooser.APPROVE_OPTION) {
                // 如果点击了"保存", 则获取选择的保存路径
                File file = fileChooser.getSelectedFile();
                msgTextArea.append("保存到文件: " + file.getAbsolutePath() + "\n\n");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    3.效果演示

    在这里插入图片描述

  • 相关阅读:
    pytest + yaml 框架 -56. 输出日志优化+allure报告优化
    mysql实际调优
    【leetcode热题】比较版本号
    高频软件测试基础面试题
    c语言练习61:malloc和free
    SSM整合
    Physics-infused Machine Learning for Crowd Simulation 论文理解
    SpringCloud Alibaba(保姆级入门及操作)
    第三章 六大设计原则 (SOLID)
    CH34X-MPHSI高速Master扩展应用—SPI设备调试
  • 原文地址:https://blog.csdn.net/qq_35132089/article/details/126900480