如何在不关闭整个程序的情况下清除 JTextFields 中的变量?
我创建了一个文本文件,在其中存储从文本字段获取的一些变量。但为了向该文本文件提交新变量,我需要关闭程序并重新打开它。 dispose();
命令关闭 JFrame
,将我带到主菜单,但再次打开菜单并提交不同的值时,上次的值已重新提交。有没有简单的方法可以修改这个?
这是我编写的 .txt 代码:
public class writeto {
static String data = AddProperty.inputdata;
BufferedWriter out;
public writeto(){
try{
out = new BufferedWriter(new FileWriter("writeto.txt", true));
out.write(data);
out.newLine();
out.close();
}catch(IOException e){
System.out.println("you have an error" + e);
}
}
}
以及在我的 addproperty
类中调用该方法的位置
submitproperty.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
housenumber1 = houseNumber.getText();
streetname1 = streetName.getText();
town1 = town.getText();
postcode1 = postcode.getText();
beds1 = beds.getText();
price1 = price.getText();
type1 = type.getText();
inputdata = housenumber1 + " " + streetname1 + " " + town1 + " " +
postcode1 +" " + beds1 + " " + price1 + " " + type1;
writeto write = new writeto();
dispose();
}
});
}
I have created a text file in which to store some variables which are taken from text fields. But in order to submit new variables to this text file, I need to close my program and reopen it. The dispose();
command closes the JFrame
taking me to my main menu but upon opening the menu again and submitting different values, the values from the previous time have been resubmitted. Is there a simple way to amend this?
Here is my write to .txt code:
public class writeto {
static String data = AddProperty.inputdata;
BufferedWriter out;
public writeto(){
try{
out = new BufferedWriter(new FileWriter("writeto.txt", true));
out.write(data);
out.newLine();
out.close();
}catch(IOException e){
System.out.println("you have an error" + e);
}
}
}
and where the method is called in my addproperty
class
submitproperty.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
housenumber1 = houseNumber.getText();
streetname1 = streetName.getText();
town1 = town.getText();
postcode1 = postcode.getText();
beds1 = beds.getText();
price1 = price.getText();
type1 = type.getText();
inputdata = housenumber1 + " " + streetname1 + " " + town1 + " " +
postcode1 +" " + beds1 + " " + price1 + " " + type1;
writeto write = new writeto();
dispose();
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该始终从菜单中创建一个带有新小部件(如文本字段)的新 JFrame。 新文本字段没有内容,如果您再次显示文本字段,它仍然会显示以前的内容。
附加说明:
writeto -> WriteTo
)writeto
类滥用构造函数。构造函数中的代码不会创建一个writeto
对象,而是将一些字符串转储到文件中。将此类代码放入方法中,而不是构造函数中。BufferedWriter
不会关闭。环顾一下 stackoverflow,很多问题/答案显示了正确的 io 关闭模式,setVisible(false)
From your menu you should always create a new JFrame with new widgets (like text fields). A new text field has no content, if you show a text field again, it will still display it's previous content.
Additional remarks:
writeto -> WriteTo
)writeto
class abuses the constructor. The code in your constructor does not create anwriteto
object but dumps some strings to a file. Put this kind of code to a method, not to a constructor.BufferedWriter
will not be closed if an exception occurs. Look around at stackoverflow, a lot of questions/answers show the correct io-closeing patternsetVisible(false)
if you just want to hide the JFrame (like "close the dialog")使用数据库而不是文本文件将使您受益匪浅。此外,您的问题表明我们不仅对 Swing 缺乏了解,而且对基本 CRUD(创建、读取、更新、删除)功能也缺乏了解。
要回答您的问题,您可以使用 textField1.setText(""); 清除文本字段。
我会阅读有关使用数据库存储数据的内容。这将使您的生活变得更加轻松。
You would benefit greatly from using a database as opposed to a text file. Further your question displays a fundamental lack of knowledge of not only Swing, but basic CRUD (Create, Read, Update, Delete) functionality.
To answer your question you can clear your text field with textField1.setText("");
I would read up on using a database for storing data. It will make life much easier for you.