[英]Java: Is there a more efficient way to write JFrame elements into an array
This is my first time posting so I apologize if the question is too vague or I do not describe my problem so well.这是我第一次发帖,如果问题太模糊或者我没有很好地描述我的问题,我深表歉意。
I am working on a Java JFrame project and have run into a small issue.我正在研究 Java JFrame 项目并遇到了一个小问题。 I need to instantiate arrays of various JFrame elements.
我需要实例化各种 JFrame 元素的 arrays 。 What I have done as of now works, however it is very inefficient and I was wondering if there is a better, easier way ti accomplish this task.
我现在所做的工作有效,但是效率很低,我想知道是否有更好、更简单的方法来完成这项任务。 The generated code generated by NetBeans instantiates my object as per there variable name that I have supplied.
NetBeans 生成的生成代码根据我提供的变量名实例化我的 object。 I am then writing a method where I am manually entering the array and which element that is equal to.
然后我正在编写一个方法,在该方法中我手动输入数组以及等于哪个元素。 Here is the auto-generated code:
这是自动生成的代码:
private javax.swing.JTextField activityStartTxt0;
private javax.swing.JTextField activityStartTxt1;
private javax.swing.JTextField activityStartTxt2;
private javax.swing.JTextField activityStartTxt3;
private javax.swing.JTextField activityStartTxt4;
The same code is used to instantiate five activityEndTxt, 5 dayDropBox, 5 timeStartDropBox and 5 timeEndDropBox.相同的代码用于实例化五个activityEndTxt、5 dayDropBox、5 timeStartDropBox 和5 timeEndDropBox。
I then write these elements into arrays然后我将这些元素写入 arrays
public void addArrays(){
dayDropBox[0] = dayDropBox0;
dayDropBox[1] = dayDropBox1;
dayDropBox[2] = dayDropBox2;
dayDropBox[3] = dayDropBox3;
dayDropBox[4] = dayDropBox4;
activityStartTxt[0] = activityStartTxt0;
activityStartTxt[1] = activityStartTxt1;
activityStartTxt[2] = activityStartTxt2;
activityStartTxt[3] = activityStartTxt3;
activityStartTxt[4] = activityStartTxt4;
timeStartDropBox[0] = timeStartDropBox0;
timeStartDropBox[1] = timeStartDropBox1;
timeStartDropBox[2] = timeStartDropBox2;
timeStartDropBox[3] = timeStartDropBox3;
timeStartDropBox[4] = timeStartDropBox4;
activityEndTxt[0] = activityEndsTxt0;
activityEndTxt[1] = activityEndsTxt1;
activityEndTxt[2] = activityEndsTxt2;
activityEndTxt[3] = activityEndsTxt3;
activityEndTxt[4] = activityEndsTxt4;
timeEndDropBox[0] = timeEndDropBox0;
timeEndDropBox[1] = timeEndDropBox1;
timeEndDropBox[2] = timeEndDropBox2;
timeEndDropBox[3] = timeEndDropBox3;
timeEndDropBox[4] = timeEndDropBox4;
}
Thanks in advance for the help在此先感谢您的帮助
I assume you keep the fields in order to be able to access them all together, probably for something like for (JTextField textField: textFeilds)....
.我假设您保留这些字段以便能够一起访问它们,可能类似于
for (JTextField textField: textFeilds)....
The way I do it is to use Arrays.asList
and hold them into a List
instead of an array.我这样做的方法是使用
Arrays.asList
并将它们保存在List
而不是数组中。 I get the advantage to stream them as well.我也获得了 stream 它们的优势。
Example (assuming you want to clear the text of all textfields):示例(假设您要清除所有文本字段的文本):
List<JTextField> textFields = Arrays.asList(field1, field2, field3, field4, field5);
textFields.stream().forEach(textField->textField.setText(""));
Use an array as shown below:使用如下所示的数组:
public void addArrays(){
DropBox[] dayDropBoxes = {dayDropBox0, dayDropBox1, dayDropBox2, dayDropBox3, dayDropBox4};
for(int i=0; i < dayDropBoxes.length && i < dayDropBox.length; i++){
dayDropBox[i] = dayDropBoxes[i];
}
//...
}
Note: I'm not sure the type of dayDropBox0
, dayDropBox1
etc. Replace DropBox[]
with the applicable type.注意:我不确定
dayDropBox0
、 dayDropBox1
等的类型。将DropBox[]
替换为适用的类型。
I don't know if this what you're specifically looking for, but you could try using for loops.我不知道这是否是您专门寻找的,但您可以尝试使用 for 循环。
for (int i = 0; i < dayDropBox.length; i++) {
dayDropBox[i] = new dayDropBox();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.