【问题标题】:JFileChooser problems with selection of foldersJFileChooser 选择文件夹的问题
【发布时间】:2015-10-13 06:47:33
【问题描述】:

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

按钮的名称从“RA1”到“RA8”。

这就是我所拥有的:

FileChooser 方法:

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);
    }
}

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

这种情况发生了 8 次,之后,当我按下“完成”按钮时,我得到如下输出:

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”作为最后一个文件夹。 现在回答我的问题:

  • 为什么会打印出last选择的路径?
  • 为什么会打印出这条路径8次
  • 我怎样才能让这个东西正常工作?

感谢您的帮助!

【问题讨论】:

  • 你能把代码贴在你要添加动作监听器的地方吗?
  • @KDM 我现在添加了设置 ActionListener 的行^^
  • 你确定RA1-RA8都是不同的对象吗?您正在使用RA1 = new JButton(...) 之类的东西?要么他们都引用同一个Button 对象,要么你正在为每个按钮调用这部分代码。
  • 考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混乱和更好的响应
  • 我“怀疑”您将ActionListener 8 次添加到每个按钮或类似的东西

标签: java swing jfilechooser filechooser


【解决方案1】:

这可能会对您有所帮助:

我创建了一个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);
      }
}

【讨论】:

  • 这解决了 3 of 'em 的问题,但如果我为 8 个这样做,它只会打印出 1、2 和 8:/
  • 您是否将ActionListener 添加到所有按钮并将所有按钮添加到doSomething()?我没有,因为在这个例子中 3 就足够了。
  • 好吧,我只是愚蠢,忘记更改一些名称,谢谢,它有效:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多