从 JFileChooser“保存”对话框中获取用户输入的文件名
这个问题的答案可能看起来很明显,但实际上我对此很挣扎。我已经搜索了 API 中的 JFileChooser 方法,并且查看了 stackoverflow 上已经提出和回答的一些问题。
我的问题是这样的。在我的程序中,我允许用户输入一个文件名,然后我将使用该文件名创建一个我将在其上写入的全新文件。如何获取用户在 JFileChooser 提供的“保存”对话框中“另存为:”标签旁边的文本字段中输入的文本?是否有 JFileChooser 方法可以让我获取用户输入的文本?或者我是否必须参加另一堂课,或者做其他事情才能获得该文本?
非常感谢所有回答的人。现在对我来说已经很晚了,这个程序将在几个小时后到期(这意味着我将再次度过一个不眠之夜)。绝望这个词可能太重了,但我已经足够接近了。
This answer to this question may seem obvious, but I'm actually struggling with it quite a bit. I've searched through JFileChooser methods in the API, and I've looked at some of the questions already asked and answered here on stackoverflow.
My question is this. In my program, I am to allow the user to type in a file name which I will then use to create a brand new file that I will write on. How do you get the text the user has entered in the textfield next to the label "Save As:" on the Save dialog box provided by JFileChooser? Is there a JFileChooser method that would allow me to get that user-inputed text? Or would I have to go through another class, or do something else to get that text?
Thank you so much, to anyone who answers. It's very late for me now, and this program is due in a few hours (meaning I'll be having another sleepless night). Desperate may be too strong a word, but I'm something close enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
......在文件名变量中,您将获得用户输入的文件名
......in filename variable you will get the file name entered by the user
打开保存文件对话框并确定用户想要保存文件后,请使用以下命令获取文件名:
After you've opened the save file dialog and determined that the user wants to save the file, grab the file name with this:
JFileChooser 有一个方法 getSelectedFile()。这是一个文件。
如果使用 showSaveDialog() 您应该能够获得 文件(文件。getName())。您可以解析它以获取用户输入的文本。 (例如删除扩展名...我不知道你想要什么:))
祝你的任务顺利。
JFileChooser has a method, getSelectedFile(). Which is a File.
If you open the dialog with showSaveDialog() you should be able to get the File from that (file.getName()). And you can parse that to get the user's entered text. (e.g. drop the extension... I don't know what you want :) )
Good luck with your assignment.