如何将 TextFields 中的文本设置为数组的数字
How to set the Text in TextFields to the numbers of an Array
我需要将 JFrame 中的每个 TextField 设置为预先生成的数字数组中的相应数字,但我不知道如何操作。
这就是我需要的样子:
这是我正在使用的 ActionListener Class:
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()。
现在,要找到最小值和最大值,您需要考虑一下。这是公司为了淘汰不良候选人而提出的一个基本问题。自己弄清楚一次,您将永远不会忘记它。想想你作为一个人会怎么做;您将当前的 biggest/smallest 与您当前正在查看的内容进行比较。
我需要将 JFrame 中的每个 TextField 设置为预先生成的数字数组中的相应数字,但我不知道如何操作。
这就是我需要的样子:
这是我正在使用的 ActionListener Class:
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()。
现在,要找到最小值和最大值,您需要考虑一下。这是公司为了淘汰不良候选人而提出的一个基本问题。自己弄清楚一次,您将永远不会忘记它。想想你作为一个人会怎么做;您将当前的 biggest/smallest 与您当前正在查看的内容进行比较。