【Java/Swing】怎样使JFileChooser 返回选择的目录而不是进入这个目录 How to make JFileChooser Returns the Selected Directory Instead Of Browsing It

【代码】

        // 新建文件选择器
        JFileChooser chooser = new JFileChooser();

        // 设定初始路径
        chooser.setCurrentDirectory(new File(folderTxt.getText()));

        // 设置只选择文件夹
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        // 弹出文件选择器
        int val=chooser.showDialog(this,"Browse");

        // 点OK时
        if(JFileChooser.APPROVE_OPTION==val){
            // 将当前选择的(目录)设置到folderTxt上
            folderTxt.setText(chooser.getSelectedFile().toString());
        }

【参考网文】

1.https://blog.csdn.net/unbelievevc/article/details/128425901

2.https://www.codenong.com/10232281/

3.https://blog.csdn.net/qq_40935723/article/details/90673198

END

posted @ 2023-05-02 22:05  逆火狂飙  阅读(30)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东