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

时间:2014-11-25 13:44:59

标签: java swing loops netbeans textfield

当我在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]+", ");
    }

输出:48,

我想要的就像当我没有使用摇摆

    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]+", ");
    }

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

对不起我的语法,我希望你们都明白......:D

2 个答案:

答案 0 :(得分:3)

试试这个:

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());

您每次都在文本字段中覆盖文本

答案 1 :(得分:0)

只需获取当前文本并按此添加,而不是覆盖已存在的文本:

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
}