简体   繁体   English

Java,我该如何重置JTextField

[英]Java, how can I reset JTextField

I made a simple search gui method to search for products in DB and it works flawlessly. 我做了一个简单的搜索gui方法来搜索数据库中的产品,它完美无缺。 However, after the search is done I'd like to reset (JTextFields which are used to get parameters of search) to blank. 但是,搜索完成后,我想将(用于获取搜索参数的JTextFields)重置为空白。 Is there a method to do this without invoking another instance? 有没有一种方法可以执行此操作而不调用另一个实例?

How about setting text content to empty strings,like this 如何将文本内容设置为空字符串,像这样

myTextField.setText("");

And further more I think you might need a class which is inherited from JTextField and you can add all sorts of methods,Getters and setters in it (such as Clear() ) which may assists you and meets your needs.. 此外,我认为您可能需要一个继承自JTextField的类,并且可以在其中添加各种方法,Getters和Setters(例如Clear()),这可能会对您有所帮助并满足您的需求。

You want to give the class that holds the JTextFields a public void reset() method, and in that method simply call setText("") on all the JTextFields that need to be cleared. 您想为持有JTextField的类提供一个public void reset()方法,在该方法中,只需对所有需要清除的JTextField调用setText("") If you place all of the JTextFields in a collection such as a List<JTextField> then you can easily close them all with a for loop: 如果将所有JTextField放置在诸如List<JTextField>类的集合中,则可以使用for循环轻松关闭它们:

public void reset() {
   for(JTextField field : fieldList) {
      field.setText("");
   }
}

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

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