gpt4 book ai didi

java - 如何在 JFileChooser 窗口中选择多个文件

转载 作者:行者123 更新时间:2023-12-02 09:39:08 28 4
gpt4 key购买 nike

我不知道如何在 JFileChooser 窗口中一次选择多个文件。我认为它已经启用,因为我在 JFileChooser 对象上使用了 setMultiSelectionEnabled(true) 方法,但是当我尝试实际选择多个时,我无法做到这一点。我尝试了单击并拖动、CTRL 和箭头键/单击、Alt 和箭头键/单击、Shift 和箭头键/单击,但仍然没有成功。我怎样才能做到这一点?

我创建 JFileChooser 的代码:仅使用一个文件时,解析方法工作正常。当按下 JFrame 中的按钮时调用该类。

public class FileChooser implements ActionListener, Runnable
{
private Parser parser = new Parser();
private static File[] selectedFiles;
private static File currentSelected;
private JFileChooser jfc;

public static File getSelectedFile()
{
return currentSelected;
}

public void actionPerformed(ActionEvent actionEvent)
{
new Thread(this).start();
}

public void run()
{
if ( Window.bFG5IsPressed() && Window.bFGAIsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setMultiSelectionEnabled(true);
int returnValue = jfc.showOpenDialog(null);

if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseAll(e.getAbsolutePath());
}
}
}
else if ( Window.bFG5IsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);

if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseFG5(e.getAbsolutePath());
}
}

}
else if ( Window.bFGAIsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);

if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseFGA(e.getAbsolutePath());
}
}

}
else
{
JOptionPane x = new JOptionPane();
x.showMessageDialog(x, "Escolher tipo de arquivo");
x.setLocation(300,300);
x.setVisible(true);
}
}

}

最佳答案

调用setMultiSelectionEnabled(true)。这是一个运行示例:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;

public class MultipleFileChooser extends JFrame {
public static void main(String[] args) {
MultipleFileChooser multipleFileChooser = new MultipleFileChooser();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
JButton button = new JButton("Open Files");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int choice = fileChooser.showOpenDialog(multipleFileChooser);
if (choice == JFileChooser.APPROVE_OPTION) {
File[] openFiles = fileChooser.getSelectedFiles();
System.out.println("Files: " + Arrays.toString(openFiles));
}
}
});
JPanel panel = new JPanel();
panel.add(button);
multipleFileChooser.add(panel);
multipleFileChooser.setSize(new Dimension(400, 400));
multipleFileChooser.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
multipleFileChooser.setVisible(true);
}
}

附上截图供OP引用: enter image description here

关于java - 如何在 JFileChooser 窗口中选择多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57242753/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com