Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSwing中的jbutton(浏览pc文件夹)_Java_Swing - Fatal编程技术网

JavaSwing中的jbutton(浏览pc文件夹)

JavaSwing中的jbutton(浏览pc文件夹),java,swing,Java,Swing,我想在swing中创建一个“浏览”按钮,当用户“浏览”浏览按钮时,他应该能够从硬盘文件夹中选择一个位置来保存文件。这是我界面设计的一部分。我该如何做? 我希望路径显示在“浏览”按钮一侧的文本框中。您应该看看Sun的API教程。这将给你几乎所有你想要完成的事情 ... public String fileID; public JTextField txtField; //Assume this is the text box you placed beside browse button publ

我想在swing中创建一个“浏览”按钮,当用户“浏览”浏览按钮时,他应该能够从硬盘文件夹中选择一个位置来保存文件。这是我界面设计的一部分。我该如何做?
我希望路径显示在“浏览”按钮一侧的文本框中。

您应该看看Sun的API教程。这将给你几乎所有你想要完成的事情

...
public String fileID;
public JTextField txtField; //Assume this is the text box you placed beside browse button
public JButton btnBrowse = JButton("Browse");

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == btnBrowse)
    {
        chooser = new JFileChooser(new File(System.getProperty("user.home") + "\\Downloads")); //Downloads Directory as default
        chooser.setDialogTitle("Select Location");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
        { 
            fileID = chooser.getSelectedFile().getPath();
            txtField.setText(fileID);
        }
    }
}
...