2013-08-21 56 views
0

我有一個JFileChooser,通過調用ExportFileChooser.createAndShowGUI()方法的按鈕單擊啓動。當我關閉JFileChooser一個名爲ExportFileChooser的新的空窗口打開時,它可以正常工作,我該如何糾正這個問題,以便它不會啓動?JFileChooser關閉啓動新窗口

下面是代碼:

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

謝謝

+0

爲了更好地提供幫助,請將您的代碼發佈爲演示您的問題的[SSCCE](http://www.sscce.org)。這允許用戶複製/粘貼並重現您的問題。 –

+1

那麼,這就是你的代碼所做的:它創建一個帶有'FileChooseExport'作爲標題的JFrame,並使其可見。爲什麼代碼會這樣做,如果你不想顯示一個框架? –

+0

@JBNizet請做出回答 – MadProgrammer

回答

3

嗯,這是你的代碼做什麼:它會創建"FileChooseExport"作爲標題的JFrame,並使其可見。爲什麼代碼會這樣做,如果你不想顯示一個框架?

您的按鈕的ActionListener的代碼應該僅僅是:

JFileChooser fcExport = new JFileChooser(); 

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

你並不需要另一個ExportFileChooser面板,放在另一個JFrame中可見,只需打開一個JFileChooser。 JFileChoose的javadoc包含示例用法,BTW。

0

感謝所有的反饋,我找到了解決方案。

在按鈕單擊我不叫CreateAndShowGUI(),但現在這個樣子創造ExportFileCHooser的新實例:

ExportFileChooser exportWindow =新ExportFileChooser();

這似乎很好地工作