简体   繁体   English

如何从同一个Object的不同变量中调用一些方法?

[英]How to call some method from different variables that from same Object?

I have some TextField, when user clicks Submit Button my program will send its data to JavaDB. 我有一些TextField,当用户单击Submit Button时,我的程序会将其数据发送到JavaDB。 After that, the program will set all TextField to be empty. 之后,程序将所有TextField设置为空。 I am using this code: 我正在使用此代码:

nama.setText("");
jenis.setText("");
bendera.setText("");
tujuan.setText("");
pelabuhanTerakhir.setText("");
latitude.setText("");
longitude.setText("");
area.setText("");
kecepatan.setText("");
haluan.setText("");
status.setText("");
keterangan.setText("");
waktu.setText(formattedDate_);

Is there any way to do that to make my code more efficient in Java? 有没有办法让我的代码在Java中更高效?

You could put your TextFields inside a list. 您可以将TextField放在列表中。 Then iterate with the help of an for each loop through this list, and invoke every time on the current entry the method setText(""). 然后在每个循环的帮助下通过此列表进行迭代,并在每个当前条目上调用方法setText(“”)。

You can make an array of TextFields and then go through it with a for loop, each time invoking setText(""). 您可以创建一个TextField数组,然后使用for循环遍历它,每次调用setText(“”)。 For example: 例如:

for(int i=0; i<yourTextFieldArray.length; i++) {
    yourTextFieldArray[i].setText("");
}  

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

相关问题 如何从java中不同包中的同一对象调用方法? - how to call a method from same object in different packages in java? 如何从不同的方法访问以某种方法创建的对象? - How to access an object which is created in some method, from different method? 如何从同一方法中查看对象的不同参数? - How to look at different parameters from an object from within the same method? 如何从java中的两个不同类调用相同的方法 - How to call same method from two different classes in java 如何从同一个java项目中的不同类调用另一个方法 - How to call another method from a different class in the same java project 如何从同一类的另一个方法中调用变量,以及如何调用该方法? - How do I call for variables from another method in same class, as well as call upon the method? 如何从带有变量的布局中调用方法 - How to call a method from layout with variables 如何让 Mockito 调用不同的方法但返回相同的对象实例 - How to make Mockito call different method but return same object instance 如何从不同的类调用方法以选择具有多个变量的用户输入 - How to call method from a different class for user input selection with multiple variables 如何从不同的服务器调用相同的wsdl - How to call same wsdl from different server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM