[英]How to set the Text in TextFields to the numbers of an Array
我需要将JFrame中的每个TextField设置为预先生成的数字数组中的相应数字,但我不知道如何。
这就是我需要的样子:
这是我正在使用的ActionListener类:
public class RandListener implements ActionListener
{
private final JTextField[] tF;
public RandListener(JTextField[] tF)
{
this.tF = tF;
}
@Override
public void actionPerformed(ActionEvent e)
{
int [] rArray = new int[10];
Random rNum = new Random();
for(int k = 0; k < rArray.length; k++)
{
rArray[k] = rNum.nextInt(100);
}
if(e.getActionCommand().equals("bRand"))
{
for(int k = 0; k < tF.length; k++)
{
tF[k].setText(/*I need to set the text of my TextFields to the numbers in the array*/);
}
}
if(e.getActionCommand().equals("bMaxMin"))
{
//I need to find the maximum and minimum of the Array here
}
}
}
阅读有关Java中的字符串如何工作的信息。 基本上,如果您想将数字转换为字符串:
tF[k].setText(/*I need to set the text of my TextFields to the numbers in the array*/);
变成:
tF[k].setText("" + rArray[k]);
我相信这是Java自动完成的; 数字装箱将原始类型转换为其各自的包装器(int-> Integer),然后在代表原始类型的Integer对象上调用toString()。
现在,要找到最小值和最大值,您将需要考虑一下。 这是公司将淘汰不良候选人的基本问题。 自己弄清楚,您将永远不会忘记。 想想您将如何以人为本? 您选择当前最大/最小,并将其与当前正在查看的内容进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.