如何使用文件对话框?
我创建了一个界面,我想添加一个允许用户打开文件的功能。我正在使用AWT。我不明白如何使用 FileDialog。您能给我一个例子或一个很好的链接来解释这一点吗?
I created an interface and I'd like to add a function that allows user to open a file. I'm using AWT. I don't understand how to use FileDialog. Can you please give me an example or a good link that explain this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
完整的代码示例,带文件过滤:
A complete code example, with file filtering:
添加到 @TheBronx 的答案 - 对我来说,
fd.setFile("*.txt");
在 OS X 上不起作用。这有效:或者作为一个奇特的 Java 8 lambda:
To add to the answer by @TheBronx - for me,
fd.setFile("*.txt");
is not working on OS X. This works:Or as a fancy Java 8 lambda:
此处有一些代码示例,演示了如何使用它各种不同的任务。
也就是说,您可能需要退一步检查 awt 是否是最适合这里工作的任务。当然,使用它而不是像 swing / swt 这样的东西有充分的理由,但如果你刚刚开始,那么 Swing,IMO 将是一个更好的选择(有更多的组件,更多的教程,并且它是一个更广泛要求使用的库这些日子。)
There's a few code samples here that demonstrate how to use it for various different tasks.
That said, you might want to take a step back and check whether awt is the best task for the job here. There are valid reasons for using it over something like swing / swt of course, but if you're just starting out then Swing, IMO would be a better choice (there's more components, more tutorials and it's a more widely requested library to work with these days.)