Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使java FileDialog在OS X中接受目录作为其文件类型?_Java_Macos_Look And Feel_Filedialog - Fatal编程技术网

如何使java FileDialog在OS X中接受目录作为其文件类型?

如何使java FileDialog在OS X中接受目录作为其文件类型?,java,macos,look-and-feel,filedialog,Java,Macos,Look And Feel,Filedialog,当我的应用程序在mac上运行时,我正在尝试从使用JFileChooser切换到使用FileDialog,以便它使用OSX文件选择器。到目前为止,我有以下代码: FileDialog fd = new FileDialog(this); fd.setDirectory(_projectsBaseDir.getPath()); fd.setLocation(50,50); fd.setFile(?); fd.setVisible(true); File

当我的应用程序在mac上运行时,我正在尝试从使用JFileChooser切换到使用FileDialog,以便它使用OSX文件选择器。到目前为止,我有以下代码:

    FileDialog fd = new FileDialog(this);
    fd.setDirectory(_projectsBaseDir.getPath());
    fd.setLocation(50,50);
    fd.setFile(?);
    fd.setVisible(true);
    File selectedFile = new File(fd.getFile());

我该怎么回答这个问题?因此,我的文件选择器将允许任何目录作为文件选择器的输入(下面的方法已经进行了检查,以确保该目录是正确的目录类型,我只希望FileDialog接受任何目录)

假设您决定使用FileDialog而不是portable JFileChooser,则需要设置系统属性,以便为目录创建FileDialog

有问题的财产是

因此,只需执行以下操作:

System.setProperty("apple.awt.fileDialogForDirectories", "true");
FileDialog fd = new FileDialog(this); 
fd.setDirectory(_projectsBaseDir.getPath()); 
fd.setLocation(50,50);
fd.setVisible(true); 
File selectedFile = new File(fd.getFile());
System.setProperty("apple.awt.fileDialogForDirectories", "false");
应该注意的是,这是不可移植的,但是,因为您正在寻找替换可移植JFileDialog的方法,所以我认为这不是一个问题

当我的应用程序在mac上运行时,我正在尝试从使用JFileChooser切换到使用FileDialog,以便它使用OSx文件选择器

我建议你尽量呆在秋千世界里,避开AWT的重磅世界。若你们的问题是这样的,那个么有一些方法可以解决Mac上Swing L&F的问题。看一看,哪些链接指向一个站点,显示如何在文件选择器中获得正确的Mac图标


请原谅我没有准确回答你的问题。如果您还有其他原因希望继续使用
FileDialog
,我将很乐意删除此帖子。

在使用最流行的解决方案一段时间后:

System.setProperty("apple.awt.fileDialogForDirectories", "true");
我无法解决本机FileDialog实现的按钮(仅英文)翻译问题

因此,我找到了一种在macOS上完全有效的解决方法:

try {
    Process process = Runtime.getRuntime().exec(new String[]{//
        "/usr/bin/osascript", //
        "-e", //
        "set selectedFolder to choose folder\n"//
        + "return POSIX path of selectedFolder"
    });
    int result = process.waitFor();
    if (result == 0) {
        String selectedFolder = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine();
        return new File(selectedFolder);
    }
} catch (Exception ex) {
}

return null;

享受吧

我同意——JFiloeChooser是一个更好的选择,他的问题听起来更像是一个L&F问题,而不是其他任何问题。Windows是否也有类似的问题?