【问题标题】:How to write value to different textfield every time i click a button?每次单击按钮时如何将值写入不同的文本字段?
【发布时间】:2020-05-20 12:49:15
【问题描述】:

我无法克服这个问题,需要您的帮助

picture

每次我单击停止按钮时,都会在上面的字段中生成一个随机数。我想要做的是将每个数字保存在左侧的字段中。文本字段的名称为 textField_0、textField_1、textField_2 等。

izvucen = rnd.nextInt(31) + 1;
randomField.setText(Integer.toString(izvucen)); 
textField_i.setText(Integer.toString(izvucen)); // critical line, need to replace "i" with something
i++;

【问题讨论】:

    标签: java swing textfield


    【解决方案1】:

    您可以在创建时将所有文本字段放在一个列表中,并通过索引访问它们

    txtFields.get(i).setText(...)
    

    或将所有字段放入地图 (Map<Integer, TextField>) 并通过 get() 访问特定字段

    mapOfTextFields.get(i).setText()
    

    【讨论】:

    • np。让我知道是否有任何问题或是否有效(接受答案或评论等)
    猜你喜欢
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多