Java / Swing:为什么我的JFileChooser开放两次?

时间:2016-02-15 14:42:02

标签: java swing jfilechooser

所以我创建了一个Java GUI应用程序,我有一个主窗体,我通过单击一个jbutton运行这个类PathBrowser,JFileChooser运行两次,我尝试从我的mainform添加opendialog所以我可以有相同的窗户上的标志。

这是我的代码:

public class PathBrowser {
public static String filepath = null;
public static void main(String[] args)
{

    JButton select = new JButton();
    JFileChooser browse = new JFileChooser();
    //add the icon of main form for JFileChooser
    //OPENS TWICE?! Error
    browse.showOpenDialog(MainForm.frame);

    //if blank goes to user/documents. Unsure about other OSes
    browse.setCurrentDirectory(new java.io.File("C:/"));
    browse.setDialogTitle("Browse Folder");
    browse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    //when clicked open (approve option)
    if (browse.showOpenDialog(select) == JFileChooser.APPROVE_OPTION){
        //folder has peen selected
        MainForm.selfolder = true;
        //add the path to the string filepath
        filepath = (browse.getSelectedFile().getAbsolutePath());
        System.out.println("The path for the server is: "+browse.getSelectedFile().getAbsolutePath());
        //add the information to the textarea
        MainForm.textArea.setText("The path for the server is: "+browse.getSelectedFile().getAbsolutePath());
    }

}

}

由于

1 个答案:

答案 0 :(得分:6)

你打电话给function enviando2(){ var formData2 = new FormData($(".formulario2")[0]); var message2 = ""; return $.ajax({ 两次,这就是为什么你得到它两次。

只需删除此行:

browse.showOpenDialog

要保留框架的图标,请替换

browse.showOpenDialog(MainForm.frame);

browse.showOpenDialog(select)