actionPerformed 可以返回值吗?

发布于 2024-08-29 13:01:56 字数 66 浏览 9 评论 0原文

在我的应用程序中,我使用 FileChooser 来选择文件。所选文件的名称应返回给另一个类。如何在日食中做到这一点?

In my application I use a FileChooser to chose a file. The name of the selected file should be returned to another class. how to do this in eclipse?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霓裳挽歌倾城醉 2024-09-05 13:01:56

当某些事件(例如单击按钮)时,actionPerformed 由事件调度线程调用,并且永远不应该直接调用它。如果您想要一个显示 FileChooser 并返回所选文件的方法,请声明另一个可以由 eventHandler 以及其他任何地方调用的方法:

public void actionPerformed(ActionEvent e) {
    File myFile = selectFile();
    doSomethingWith(myFile);
}

public File selectFile() {
    int returnVal = fc.showDialog(FileChooserDemo2.this,
                                  "Attach");
    //Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        return fc.getSelectedFile();
    } else {
        return null;
    }
}

actionPerformed is called by the event dispatch thread when some event (a button was clicked for instance) and it should never be invoked directly. If you want a method that shows a FileChooser and returns the selected file, then declare another method that can be called by an eventHandler as well as anywhere else:

public void actionPerformed(ActionEvent e) {
    File myFile = selectFile();
    doSomethingWith(myFile);
}

public File selectFile() {
    int returnVal = fc.showDialog(FileChooserDemo2.this,
                                  "Attach");
    //Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        return fc.getSelectedFile();
    } else {
        return null;
    }
}
橘香 2024-09-05 13:01:56

查看 FileChooserDemo 和 FileChooserDemo2 此处 了解 FileChooser 的使用方法。

这是代码的相关摘录:

    public void actionPerformed(ActionEvent e) {
    //Set up the file chooser.
    if (fc == null) {
        fc = new JFileChooser();

    //Add a custom file filter and disable the default
    //(Accept All) file filter.
        fc.addChoosableFileFilter(new ImageFilter());
        fc.setAcceptAllFileFilterUsed(false);

    //Add custom icons for file types.
        fc.setFileView(new ImageFileView());

    //Add the preview pane.
        fc.setAccessory(new ImagePreview(fc));
    }

    //Show it.
    int returnVal = fc.showDialog(FileChooserDemo2.this,
                                  "Attach");

    //Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        log.append("Attaching file: " + file.getName()
                   + "." + newline);
    } else {
        log.append("Attachment cancelled by user." + newline);
    }
    log.setCaretPosition(log.getDocument().getLength());

    //Reset the file chooser for the next time it's shown.
    fc.setSelectedFile(null);
}

Have a look at FileChooserDemo and FileChooserDemo2 here for how FileChooser is used.

Here is a relevant excerpt of the code:

    public void actionPerformed(ActionEvent e) {
    //Set up the file chooser.
    if (fc == null) {
        fc = new JFileChooser();

    //Add a custom file filter and disable the default
    //(Accept All) file filter.
        fc.addChoosableFileFilter(new ImageFilter());
        fc.setAcceptAllFileFilterUsed(false);

    //Add custom icons for file types.
        fc.setFileView(new ImageFileView());

    //Add the preview pane.
        fc.setAccessory(new ImagePreview(fc));
    }

    //Show it.
    int returnVal = fc.showDialog(FileChooserDemo2.this,
                                  "Attach");

    //Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        log.append("Attaching file: " + file.getName()
                   + "." + newline);
    } else {
        log.append("Attachment cancelled by user." + newline);
    }
    log.setCaretPosition(log.getDocument().getLength());

    //Reset the file chooser for the next time it's shown.
    fc.setSelectedFile(null);
}
七月上 2024-09-05 13:01:56

假设类“A”包含显示文件选择器的代码,而类“B”需要该值,则以下内容将满足您的需要。

class A {
    private PropertyChangerSupport changer = new PropertyChangerSupport(this);
    private File selectedFile = null;

    public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
        changer.addPropertyChangeListener(property, listener);
    }

    public void removePropertyChangeListener(String property, PropertyChangeListener listener) {
        changer.removePropertyChangeListener(property, listener);
    }

    public void actionPerformed(ActionEvent evt) {
        // Prompt the user for the file
        selectedFile = fc.getSelectedFile();
        changer.firePropertyChange(SELECTED_FILE_PROP, null, selectedFile);
    }
}

class B {
    public B(...) {
        // ...
        A a = ...
        a.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChanged(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(A.SELECTED_FILE_PROP)) {
                    File selectedFile = (File)evt.getNewValue();
                    // Do something with selectedFile
                }
            }});
    }
}

Assuming class "A" contains the code to show the file chooser and class "B" needs the value, the following will do what you need.

class A {
    private PropertyChangerSupport changer = new PropertyChangerSupport(this);
    private File selectedFile = null;

    public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
        changer.addPropertyChangeListener(property, listener);
    }

    public void removePropertyChangeListener(String property, PropertyChangeListener listener) {
        changer.removePropertyChangeListener(property, listener);
    }

    public void actionPerformed(ActionEvent evt) {
        // Prompt the user for the file
        selectedFile = fc.getSelectedFile();
        changer.firePropertyChange(SELECTED_FILE_PROP, null, selectedFile);
    }
}

class B {
    public B(...) {
        // ...
        A a = ...
        a.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChanged(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(A.SELECTED_FILE_PROP)) {
                    File selectedFile = (File)evt.getNewValue();
                    // Do something with selectedFile
                }
            }});
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文