我有一个 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/