执行文件“保存”像记事本
我想创建一个像记事本一样的文本编辑器(使用java/swing)。为此我需要实现保存文件。意味着如果用户第一次单击“保存”,则应出现用于获取文件名、文件扩展名的对话框。但是,如果他再次单击相同的“保存”按钮,则文件应该保存,而不会出现“保存对话框”。
谢谢
I want to create a text editor (using java/swing) like notepad. For this I need the implementation of saving file. mean if the user clicks on "save" first time then the dialog should appear for taking file-name, file-extension. But if he clicks again on same "save" button then the file should save without appearing the "save-dialog-box".
Thanx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需在您的应用程序中跟踪所选的名称即可。如果您的应用程序已存储名称,请在后续保存中重新使用它。
Just keep track of the chosen name within your application. If your application has stored a name, re-use it for subsequent saves.
您可以查看该文件是否已命名。您可能从“Unnamed1”文件开始。这是您要显示保存对话框的位置。如果您打开了一个已经存在的文件或者您已经保存了该文件,您将知道其名称并可以直接保存。
You can just look whether the file was already named. You probably start with an "Unnamed1" file in the beginning. This is where you want to display the save dialog. In case you opened a file that already existed or you already saved the file you would know its name and can save directly.
如果您使用单例
JFileChooser
,它将保留最后选择的文件路径/名称,直到您再次弹出对话框。这样做可能会防止您可能遇到的文件名同步问题。例如,用户第一次保存,输入他想要保存的文件名,然后一分钟后改变主意并决定另存为新的文件名。如果您使用单例JFileChooser
,则可以在执行实际保存时引用它,因为它始终具有最近选择的文件名。If you use a singleton
JFileChooser
, it will hold the last-selected file path/name until you pop the dialog again. Doing it this way might prevent filename synchronization issues you might encounter otherwise. For example, the user saves the first time, enters the filename he wants to save as, then a minute later changes his mind and decides to Save As to a new filename. If you use a singletonJFileChooser
, you can just reference it when performing the actual save because it will always have the most recently selected filename.有关使用 Java 文件选择器的一般帮助,请查看 Sun 教程
如何使用文件选择器
For general help on using the Java File chooser check out the Sun Tutorial
How to Use File Choosers