简体   繁体   English

如何获取“JTextField 数组”的值并将其存储在 Array Integer 中?

[英]How can I get the value of the 'Array of JTextField' and store it in an Array Integer?

I have two arrays of JTextField and I want to store it into another array.我有两个JTextField数组,我想将它存储到另一个数组中。

JTextField[] proText, atText; 


int[] burst, arrive;

I have tried the usual way of the passing the value of the array我已经尝试了传递数组值的常用方法

while(true){
    if(atText[lowerBound].getText() != " " && proText[lowerBound].getText() != " "){
        bt = proText[lowerBound].getText();
        at = atText[lowerBound].getText();
        burst[lowerBound] = Integer.parseInt(bt);
        arrive[lowerBound] =Integer.parseInt(at);
        break;
    }else 
        break;
}

***the 'lowerBound' is being incremented when I click the button. ***当我单击按钮时,'lowerBound' 正在增加。

BTW this is dynamic, in which it adds another text field when I am clicking the button.顺便说一句,这是动态的,当我单击按钮时,它会添加另一个文本字段。 The problem here is that only the last number is displayed and the rest are zero.这里的问题是只显示最后一个数字,其余数字为零。 Is there another way to get the value of a text field?有没有另一种方法来获取文本字段的值?

I read your code, the problem is simple, each time you hit one of the buttons, you re-init your arrays.我读了你的代码,问题很简单,每次你按下一个按钮,你就重新初始化你的数组。 Of cours the previous values will be lost.当然,之前的值将丢失。

A quick fix is to copy the values first and then store the new value in your actionPerformed method快速解决方法是先复制值,然后将新值存储在actionPerformed方法中

int[] atTemp = new int[arrsize];
int[] btTemp = new int[arrsize];
for (int i = 0; i < arrsize - 1; i++) {
    atTemp[i] = at[i];
    btTemp[i] = bt[i];
}
at = atTemp;
bt = btTemp;

But if you don't know the size of your arrays then use ArrayList , then you don't have to re-init your array each time you hit the buttons.但是,如果您不知道数组的大小,则使用ArrayList ,那么每次点击按钮时都不必重新初始化数组。

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

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