JFileChooser 文件夹选择问题

JFileChooser problems with selection of folders

最近我不得不在我的一个项目中使用文件选择器。我做了一个有 8 个按钮的框架,每个按钮打开一个文件选择器来设置一些字符串。

按钮的名称来自 "RA1" - "RA8"。

这就是我所拥有的:

文件选择器方法:

public File openDataBrowser() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));

    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int result = fileChooser.showOpenDialog(fileChooser);
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        return selectedFile;
    }

    return new File("");
}

动作侦听器:

public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(RA1)) path1 = openDataBrowser().getAbsolutePath().replace("\", "/");
    else if (e.getSource().equals(RA2)) path2 = openDataBrowser().getAbsolutePath().replace("\", "/");
    else if (e.getSource().equals(RA3)) path3 = openDataBrowser().getAbsolutePath().replace("\", "/");
    else if (e.getSource().equals(RA4)) path4 = openDataBrowser().getAbsolutePath().replace("\", "/");
    else if (e.getSource().equals(RA5)) path5 = openDataBrowser().getAbsolutePath().replace("\", "/");
    else if (e.getSource().equals(RA6)) path6 = openDataBrowser().getAbsolutePath().replace("\", "/");
    else if (e.getSource().equals(RA7)) path7 = openDataBrowser().getAbsolutePath().replace("\", "/");
    else if (e.getSource().equals(RA8)) path8 = openDataBrowser().getAbsolutePath().replace("\", "/");
    else if (e.getSource().equals(finish)) {
        System.out.println(path1);
    }
}

所以首先我想选择文件,然后我想将数据发送到另一个 class,为了测试目的我只想打印路径,但它不会真正起作用。单击其中一个按钮时,文件选择器会弹出,但在单击 "open" 后,它只会弹出另一个按钮。

这发生了 8 次,之后,当我按下按钮 "finish" 时,我得到如下输出:

C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8

我的文件夹的名称也来自 "RA1" - "RA8"。

我选择 "RA8" 作为最后一个文件夹。 现在回答我的问题:

感谢您的帮助!

这可能对您有帮助:

我做了一个 ActionListener 调用方法 doSomething() 并将其调用 JButton 作为参数

ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        doSomething((JButton)e.getSource());
    }
};

ActionListener 将添加到所有 JButtons

RA1.addActionListener(al);
RA2.addActionListener(al);
...
RA8.addActionListener(al);
finish.addActionListener(al);

doSomething() 看起来像这样(为了保持简洁而缩短为 3 个按钮):

protected void doSomething(JButton src) {
        if (src.equals(RA1)) path1 = openDataBrowser().getAbsolutePath().replace("\", "/");
        else if (src.equals(RA2)) path2 = openDataBrowser().getAbsolutePath().replace("\", "/");
        else if (src.equals(RA3)) path3 = openDataBrowser().getAbsolutePath().replace("\", "/");
        else if (src.equals(finish)) {
             System.out.println(path1);
             System.out.println(path2);
             System.out.println(path3);
      }
}