0

我有一个 JFileChooser,它通过单击调用 ExportFileChooser.createAndShowGUI() 方法的按钮启动。它工作正常,当我关闭 JFileChooser 时,会打开一个名为 ExportFileChooser 的新空窗口,我该如何更正它以使其无法启动?

这是代码:

package org.annotationRoi3D.io;

import java.io.*;
import java.awt.*;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * This creates a dialog window for exporting 
 * and importing XML files.
 */
public class ExportFileChooser extends JPanel {

    private static final long serialVersionUID = 1L;
    public static File ExportFile;
    JFileChooser fcExport;

    public ExportFileChooser() {
        super(new BorderLayout());
        fcExport = new JFileChooser();

        int returnValExport = fcExport.showSaveDialog(ExportFileChooser.this);
        if (returnValExport == JFileChooser.APPROVE_OPTION) {
            ExportFile = fcExport.getSelectedFile();
            org.annotationRoi3D.io.ExportXML.OutputXML();
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frameExport = new JFrame("FileChooserExport");
        frameExport.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frameExport.add(new ExportFileChooser());

        //Display the window.
        frameExport.pack();
        frameExport.setVisible(true);
    }
}

谢谢

4

2 回答 2

3

好吧,这就是您的代码所做的:它创建一个带有"FileChooseExport"标题的 JFrame,并使其可见。如果您不想显示框架,为什么代码会这样做?

按钮的 ActionListener 的代码应该是:

JFileChooser fcExport = new JFileChooser();

int returnValExport = fcExport.showSaveDialog(thePanelContainingTheButton);
if (returnValExport == JFileChooser.APPROVE_OPTION) {
    ...
}

您不需要另一个 ExportFileChooser 面板,放置在另一个可见的 JFrame 中,只需打开一个 JFileChooser。JFileChoose 的 javadoc 包含示例用法,顺便说一句。

于 2013-08-21T10:09:56.547 回答
0

Thanks for all the feedback, I have found the solution.

Upon button click I do not call CreateAndShowGUI() but now create a new instance of ExportFileCHooser like this:

ExportFileChooser exportWindow = new ExportFileChooser();

that seems to work perfectly

于 2013-08-22T01:52:32.163 回答