简体   繁体   English

如何使多个JTextFields写入一个文件

[英]How to make multiple JTextFields write to one file

I have a project involving a recipe book that saves info like ingredients, utilities, etc. and i need to make the fields that those details are entered in, save that text to a file. 我有一个涉及食谱书的项目,该书可以保存诸如配料,实用程序等信息,并且我需要在输入那些详细信息的字段中将该文本保存到文件中。 Each field is a JTextField. 每个字段都是一个JTextField。 The only thing being saved is text to a .txt. 保存的唯一内容是文本到.txt。 *the class is called recipereader, but i am only writing to the file*ignore the name* If i could get a response that showed what the code would look like that would be much appreciated. *该类称为配方阅读器,但我只写文件*忽略名称*如果我能得到表明代码看起来像的响应,将不胜感激。 Also, i am using bufferedwriter so id like to keep within that unless its not possible. 另外,我正在使用bufferedwriter,所以id希望保持在该范围内,除非不可能。

PrintWriter outputStream=new PrintWriter("recipe001.txt");
outputStream.println(recipeName.getText());
outputStream.close();

recipeName is the JTextField.(and i know the code is wrong). recipeName是JTextField。(我知道代码是错误的)。 i would like the content of each JTextField to be presented with one line, so thats why im doing println. 我希望每个JTextField的内容都用一行显示,所以这就是为什么我正在做println. Its quite simple but i do not know how to do the getText from the JTextField My frame is in another class from this code here 它很简单,但我不知道如何从JTextField执行getText 。我的框架在此代码的另一个类中

Update: 更新:

RecipeReader (or maybe it should be RecipeWriter ?) should not know that the data comes from a Swing GUI. RecipeReader (或者应该是RecipeWriter吗?)不应该知道数据来自Swing GUI。 In other words, you need to design its methods to take String parameters with the data you want to store. 换句话说,您需要设计其方法以对要存储的数据使用String参数。 Then the GUI code will call getText() from on each JTextField and send the String s to the method that writes the file. 然后,GUI代码将从每个JTextField上调用getText() ,并将String发送到写入文件的方法。

You also need to be careful that when you open a file, it will be erased if it already exists. 您还需要注意,当您打开文件时,如果该文件已经存在,将被删除。 You probably should open the file once when the program starts and only close it when your program ends. 您可能应该在程序启动时打开该文件一次,而仅在程序结束时将其关闭。 That way you can continue to write to the file without deleting anything which you previously wrote to the file. 这样,您可以继续写入文件,而不会删除以前写入文件的任何内容。

The other option is to open the file in "append" mode. 另一个选项是在“附加”模式下打开文件。 I won't get into any more details here as there are plenty of online resources which explain how to do this. 由于这里有大量的在线资源介绍了如何执行此操作,因此在此不再赘述。

Original Answer: 原始答案:

This will take some work on your part since JTextField s have no concept of a "file" built into them. 由于JTextField没有内置到其中的“文件”的概念,因此您将需要做一些工作。 You will need to open a file, get the contents of each text filed, and write them to the file. 您将需要打开一个文件,获取每个文本的内容,并将它们写入文件。 Be sure to close the file when you are done. 完成后,请确保关闭文件。

If you have trouble with any of these steps, please come back with a more specific question. 如果您在执行上述任何步骤时遇到问题,请再提出一个更具体的问题。 Be sure to include code to illustrate what you have tried so far. 确保包含代码来说明您到目前为止已经尝试过的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM