简体   繁体   English

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

[英]Java/Swing: Why is my JFileChooser opening twice?

So I have created a Java GUI application, I have a main form, and I run this class PathBrowser by clicking a jbutton, The JFileChooser runs twice though, I have tried adding the opendialog from my mainform so I can have the same logo on the window. 所以我创建了一个Java GUI应用程序,我有一个主窗体,然后通过单击jbutton运行该类PathBrowser,尽管JFileChooser运行了两次,但我尝试从我的主窗体中添加opendialog,这样我就可以在窗口。

Here's my code: 这是我的代码:

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

}

}

Thanks 谢谢

You are calling browse.showOpenDialog twice, that's why you get it twice. 您两次调用browse.showOpenDialog ,这就是为什么要两次调用它。

Just remove this line : 只需删除此行:

browse.showOpenDialog(MainForm.frame);

And to keep the frame's icon, replace 并保留框架的图标,请更换

browse.showOpenDialog(select)

with

browse.showOpenDialog(MainForm.frame)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM