使用Java在Windows资源管理器中获取选定的文件路径

时间:2023-01-17 10:04:13

I'm developping a Java application in which the user is able to change the background of a JFrame using a picture from his computer.

我正在开发一个Java应用程序,用户可以使用计算机中的图片更改JFrame的背景。

In order to do that I'm trying to get the path name of the picture he selects in windows explorer.

为了做到这一点,我试图获取他在Windows资源管理器中选择的图片的路径名称。

I use this code to open the explorer but I can't figure out what I got to do next to get the selected file path...

我使用此代码打开资源管理器,但我无法弄清楚接下来我要做什么来获取所选的文件路径...

p = new ProcessBuilder("explorer.exe", "/select,C:\\directory\\selectedFile").start();

Any ideas ?

有任何想法吗 ?

Thanks.

4 个解决方案

#1


0  

You should use a JFileChooser instead.

您应该使用JFileChooser。

Then retrieve the selected file as a File object using the getSelectedFile() method.

然后使用getSelectedFile()方法将所选文件检索为File对象。

#2


0  

Better option is JFileChooser for selecting a file.

更好的选择是JFileChooser用于选择文件。

#3


-1  

I would suggest using JFileChooser.

我建议使用JFileChooser。

#4


-1  

That may be possible, but it is much easier to use a JFileChooser. You can set the look and feel to mimic the current environment (Windows, in your case). I recommend googling JFileChooser as there are tons of examples (including in the API).

这可能是可能的,但使用JFileChooser要容易得多。您可以设置外观以模仿当前环境(Windows,在您的情况下)。我推荐谷歌搜索JFileChooser,因为有大量的例子(包括在API中)。

#1


0  

You should use a JFileChooser instead.

您应该使用JFileChooser。

Then retrieve the selected file as a File object using the getSelectedFile() method.

然后使用getSelectedFile()方法将所选文件检索为File对象。

#2


0  

Better option is JFileChooser for selecting a file.

更好的选择是JFileChooser用于选择文件。

#3


-1  

I would suggest using JFileChooser.

我建议使用JFileChooser。

#4


-1  

That may be possible, but it is much easier to use a JFileChooser. You can set the look and feel to mimic the current environment (Windows, in your case). I recommend googling JFileChooser as there are tons of examples (including in the API).

这可能是可能的,但使用JFileChooser要容易得多。您可以设置外观以模仿当前环境(Windows,在您的情况下)。我推荐谷歌搜索JFileChooser,因为有大量的例子(包括在API中)。