JAVA应用打开文件对话框并输出选择的目录(简洁实用)

package com.zyq;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class XuanMulu {
    //打开文件目录对话框的方法,直接运行即可
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "JPG & GIF Images", "jpg", "gif");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(chooser);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());//clooser.getSelectedFile().getName()为点击的文件名
        }
    }
}//运行后如图

 




 

转载于:https://www.cnblogs.com/hssmyyq/p/10178888.html

你可能感兴趣的:(java,操作系统)