- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 Java 构建一个简单的程序,而且我对 GUI 还很陌生。我正在尝试打开一个文件对话框来选择一个目录并使用它的路径将文件发送到选定的目录。但是,它不适用于 FileDialog。
现在,我尝试了 JFileChooser,它一直挂起,并且不像 FileDialog 那样显示完整的 Mac OS X 对话框,我更喜欢使用后者。下面是我的文件对话框的代码。当我从对话框中选择目录时,如何获取所选目录并将其打印出来?我花了 2 天的时间研究,只是找不到一个可以工作并显示完整 MAC OS X 对话框的好解决方案。
String osName = System.getProperty("os.name");
String homeDir = System.getProperty("user.home");
File selectedPath = null;
final JFileChooser fc = new JFileChooser();
if (osName.equals("Mac OS X")) {
System.setProperty("apple.awt.fileDialogForDirectories", "true");
FileDialog fd = new FileDialog(new Frame(), "Choose a file", FileDialog.LOAD);
fd.setDirectory(homeDir);
fd.setVisible(true);
String filename = fd.getDirectory();
selectedPath = new File(filename);
if (filename == null) {
continue;
} else {
save_location = filename;
dout.writeUTF("200"); //Status OK
dout.flush();
System.out.println(filename);
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
}
最佳答案
How can I get the selected directory and print it out when I select it from the dialog?
使用fd.getFile()
获取目录名称,例如
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
public class Main {
public static void main(String[] args) {
String osName = System.getProperty("os.name");
String homeDir = System.getProperty("user.home");
File selectedPath = null;
if (osName.equals("Mac OS X")) {
System.setProperty("apple.awt.fileDialogForDirectories", "true");
FileDialog fd = new FileDialog(new Frame(), "Choose a file", FileDialog.LOAD);
fd.setDirectory(homeDir);
fd.setVisible(true);
String fileName = fd.getFile();
System.out.println(fileName);
File file;
if (fileName != null) {
file = new File(fd.getDirectory() + fileName);
System.out.println("You selected "+file.getAbsolutePath());
} else {
System.out.println("You haven't selected anything");
}
}
}
}
输出:当我选择桌面
然后按打开
Desktop
You selected /Users/arvind.avinash/Desktop
注释:
fd.getDirectory()
获取所选目录的父目录的路径,即我的示例中的 /Users/arvind.avinash/
。fd.getFile()
获取所选目录的名称,即我的示例中的 Desktop
。fd.getDirectory() + fd.getFile()
来获取所选目录的完整路径,即我的/Users/arvind.avinash/Desktop
示例。关于java - 有没有办法从Java中的FileDialog获取目录路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60587552/
我正在 Access 数据库中制作表单我需要多次打开文件对话框窗口。我只是不明白为什么在我更改了几次选项值并打开文件对话框窗口后它没有更改过滤器。 Public Sub Command17_Click
我正在使用 Java 开发一个 socket 程序。我在后台运行带有套接字服务器的GUI。套接字服务器正在运行一个线程,每 10 毫秒检查一次套接字消息。它们都运行良好,但是当我尝试在 gui 中打开
我使用以下代码遇到了奇怪的行为: FileDialog openFileDialog1; // ... openFileDialog1.CustomPlaces.Add(@"C:\whatever\"
谁能给我解释一下为什么FileDialog实现两个构造函数,一个用于 Frame,一个用于 Dialog?我的意思是,他们为什么不利用继承的好处而只是使用 Window 创建一个构造函数? ?我问这个
我有一个使用 FileDialog 浏览文件的按钮。我需要读取我选择的文件中的所有行,我尝试了很多方法,但对我没有任何作用:( 我有另一个按钮,当我按下它时,它会读取文件并打印它。 B
我想显示一个 Java FileDialog 并将其初始大小设置为合理的值(它不显示最大化按钮,很多用户可能不知道可以使用 alt-space X 代替)。我尝试了 setBounds 但它没有做任何
在开发WinForms应用程序时,我遇到了OpenFileDialog和SaveFileDialog控件中的一个错误。 Google搜索发现只有一个发现相同问题的其他人,但是没有提供解决方案或解决方法
我在 StackOverflow 上找到了这段代码: Dim fd As Office.FileDialog Set fd = Application.FileDialog(msoFileDi
您好,我正在尝试从设置路径中基于 FileDialog 的文件夹导入文本文件?我有一个导入文本文件的代码,但它只打开一个通用的 C:\\ 路径,我应该如何修改下面的代码才能打开指定路径的文件夹? Su
我试图在 QtQuick 中为 FileDialog 设置默认起始目录,但它不起作用(意味着它总是打开可执行文件的目录): property url defPath : "/home/saeid/Wo
我正在创建一个基于 SWT 的应用程序。我有一个名为“Import”的按钮,单击此按钮将打开一个 swt FileDialog 来选择文件。当此文件对话框打开时,我有一个独特的要求,我只想显示特定文件
我试图在 QtQuick 中为 FileDialog 设置默认起始目录,但它不起作用(意味着它总是打开可执行文件的目录): property url defPath : "/home/saeid/Wo
我需要打开一个 FileDialog,以便在使用 Java 7 的 Windows 7 Home 上使用 SWT 打开文件。我需要在按下 BrowseBtn1 按钮 (SWT) 时打开此 FileDi
我可以使用 JFileChooser 给出文件扩展名。示例: JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter fi
当我想在从控制台获得一些输入后打开文件对话框时,它失败了。请参阅下面的代码。当我第一次调用 openFileDialog 然后 chooseOption 时,它工作正常。有人知道这是怎么可能的吗? p
我确信这些都是非常白痴的问题...但我以前从未处理过 FileDialog,而且我似乎无法让我的编码工作。 这是我的 JButton 监听器,我知道它进入是因为弹出了一个 FileDialog: pu
如果我有这样的过滤器字符串(FileDialog 的过滤器): "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*" C# 中是否有
JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); add(menubar,BorderLayout.NORTH);
我会将 FileDialog 组件本地化为英语。我的默认语言是意大利语。可能吗? 我使用 FileDialog 的项目是一个 Eclipse 插件项目。 我找到了仅适用于 JFileChooser 组
当窗口对话框打开时,我无法选择多个文件。 void setup() { size(100,100); select_multi_files_via_FileDialog(); } import
我是一名优秀的程序员,十分优秀!