java 使用 JFileChooser 添加多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13060371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding multiple files with JFileChooser
提问by rogerthat
I'm looking to make an application that allows you to attach files from your computer. For example, when the JFileChooser opens you have the option to select multiple files and when you click "add" it adds it to the window and you can then have different for what you want to do with those files. Similar to attaching files in an email. All the code I have to show is the JFileChooser because I figure I should learn how to do this before I go any further.
我正在寻找一个应用程序,允许您从您的计算机附加文件。例如,当 JFileChooser 打开时,您可以选择多个文件,当您单击“添加”时,它会将其添加到窗口中,然后您可以对这些文件进行不同的操作。类似于在电子邮件中附加文件。我必须展示的所有代码都是 JFileChooser,因为我认为我应该在继续之前学习如何做到这一点。
Thanks in advance
提前致谢
import javax.swing.JFileChooser;
import java.io.File;
public class Locket {
public static void main(String[] args)
{
JFileChooser chooser = new JFileChooser();
File f = new File("Desktop");
chooser.setCurrentDirectory(f);
chooser.showOpenDialog(null);
}
}
}
回答by Juvanis
JFileChooser API already provides a method named getSelectedFiles()
. You can use it for multiple selection. It returns an array of file objects, i.e. File[]
.
JFileChooser API 已经提供了一个名为getSelectedFiles()
. 您可以使用它进行多项选择。它返回一个文件对象数组,即File[]
.
回答by MadProgrammer
You need to use JFileChooser#setMultiSelectionEnabled
and set it to true. This will allow the user to be able to select multiple files
您需要使用JFileChooser#setMultiSelectionEnabled
并将其设置为true。这将允许用户能够选择多个文件