简体   繁体   English

如何将循环值设置为文本字段?

[英]how to set loop value to textfield?

when i use swing in netbeans 当我在netbeans中使用swing

    Random acak = new Random ();
    int max = 99;
    int []hasilRandom = new int[9];
    for (int i = 0; i <hasilRandom.length; i++){
        hasilRandom[i] = acak.nextInt(max);
        jTextRandom.setText(hasilRandom[i]+", ");
    }

output : 48, 输出: 48,

what i want is like when i didnt use swing 我想要的是当我不使用秋千时

    Random acak = new Random ();
    int max = 99;
    int []hasilRandom = new int[9];
    for (int i = 0; i <hasilRandom.length; i++){
        hasilRandom[i] = acak.nextInt(max);
        System.out.print(hasilRandom[i]+", ");
    }

output: 48, 30, 98, 78, 70, 16, 40, 2, 7, 输出: 48, 30, 98, 78, 70, 16, 40, 2, 7,

Sorry for my grammar, i hope u all understand.. :D 对不起,我的语法,我希望大家都明白..:D

Try this: 尝试这个:

Random acak = new Random ();
int max = 99;
int []hasilRandom = new int[9];
StringBuilder text = new StringBuilder();
for (int i = 0; i <hasilRandom.length; i++){
    hasilRandom[i] = acak.nextInt(max);
    text.append(hasilRandom[i]+", ");
}
jTextRandom.setText(text.toString());

You just overwrited text in textfield every time 您每次都覆盖文本字段中的文本

Just get the current text and append it like this instead of overwriting what is there already: 只需获取当前文本并像这样添加它,而不是覆盖已经存在的内容:

Random acak = new Random ();
int max = 99;
int []hasilRandom = new int[9];
for (int i = 0; i <hasilRandom.length; i++){
    hasilRandom[i] = acak.nextInt(max);
    jTextRandom.setText(jTextRandom.getText() + ", " + hasilRandom[i]); //use getText to append new value to current value
}

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

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