java文件选择器的使用实例源码
//首先定义一个文件选择器:
private JFileChooser jfcDir = new JFileChooser();
//再编写相应的触发事件:
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == chooseDirButton)
{
jfcDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只选择目录
jfcDir.setDialogTitle(“选择目录”);
int result = jfcDir.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION)
{
File f = jfcDir.getSelectedFile();
trainDirTextField.setText(f.getAbsolutePath()); //在相应文本框显示所选路径
}
else
{
return; //若点击取消则直接关闭窗口
}
}
}
本文链接地址: java文件选择器的使用实例