我想在摇摆中创建一个"浏览"按钮,当用户"浏览"浏览按钮时,他可以从他的硬盘文件夹中选择一个位置来保存文件.这是我界面设计的一部分.我做到了吗?我希望路径显示在浏览按钮一侧的文本框中.
...
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);
}
}
}
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5846 次 |
| 最近记录: |