java - JFileChooser 关闭启动新窗口

标签 java swing

我有一个 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);
    }
}

谢谢

最佳答案

嗯,这就是您的代码的作用:它创建一个以 "FileChooseExport" 作为标题的 JFrame,并使其可见。如果您不想显示框架,为什么代码要这样做?

按钮的 ActionListener 的代码应该是:

JFileChooser fcExport = new JFileChooser();

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

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

关于java - JFileChooser 关闭启动新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18354386/

相关文章:

Java - 向 JFrame 添加组件

java - 通过按钮在 JSF 站点之外导航的选项

java - 在 Shell 脚本中使用带空格的路径

java - 如何在 Java 中创建一个新的 zip 文件并向其中添加一个大的文件目录?

java - 如何在具有 GridLayout 的 JPanels 中居中对齐所有内容

java - BufferedImage 旋转后被切断

java - Jtable保留列宽

java - 在 Java 中从 String 常量映射到 int 常量的更好方法

java - 如何在没有 JSTL 的情况下迭代 JSP 上的数组列表

java - 为什么我的组件没有包装在 Swing 的 FlowLayout 中?