如何使用 JFileChooser.showSaveDialog(...) 设置建议的文件名? [英] How do I set a suggested file name using JFileChooser.showSaveDialog(...)?

查看:40
本文介绍了如何使用 JFileChooser.showSaveDialog(...) 设置建议的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JFileChooser 似乎缺少一个功能:一种在保存文件时建议文件名的方法(通常会被选中,以便在用户开始输入时将其替换).

The JFileChooser seems to be missing a feature: a way to suggest the file name when saving a file (the thing that usually gets selected so that it would get replaced when the user starts typing).

有没有办法解决这个问题?

Is there a way around this?

推荐答案

如果我没理解错,你需要使用 setSelectedFile 方法.

If I understand you correctly, you need to use the setSelectedFile method.

JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setSelectedFile(new File("fileToSave.txt"));
jFileChooser.showSaveDialog(parent);

该文件不需要存在.

如果传递带有绝对路径的文件,JFileChooser 将尝试将自身定位在该目录中(如果存在).

If you pass a File with an absolute path, JFileChooser will try to position itself in that directory (if it exists).

这篇关于如何使用 JFileChooser.showSaveDialog(...) 设置建议的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆