java中JFileChooser小问题

来源:百度知道 编辑:UC知道 时间:2024/06/08 14:18:00
JFileChooser fileChooser = new JFileChooser(); 中怎么让打开后获取选定文件名称???求解。。。。。

JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File selectdFile = chooser.getSelectedFile();
}
第二行代码参数parent是文件选择器的父窗体,可以为null,也就是默认为整个屏幕,会显示到屏幕的中间,如果你定义了一个窗体Frame,参数设为Frame,那么它将显示到窗体的中间,这个参数决定它弹出的位置。

另外文件选择器可以过滤显示,添加一个javax.swing.filechooser.FileFilter
具体操作看JDK文档
希望对你有用!!