单击时如何更改 jButton 值

How to change jButton value when clicked

我在编译时一直出错。

我不知道如何使用列表生成的整数并在单击后将其显示到 jButtons 中。

 public static void Random ()
{
    int Rand [] = new int [49];
    for (int b = 0; b < 49; b++)
        Rand [b] = b;

    List<Integer> alist = Arrays.stream(Rand)
                                        .boxed()
                                        .map (x -> x + 1)
                                        .collect (Collectors.toList());                                 

    Collections.shuffle(alist);
}

private class HandleTextField implements ActionListener
{   
   @Override
   public void actionPerformed(ActionEvent event)
   {
       for (int a = 0; a < 49; a++)
       {
        if (event.getSource() == jbArray[a]) 
        {
            //error on this line "alist cannot be resolved to a variable"
            jbArray[a].setText(alist);
        }   
     }
   }
} 

所以,你这里有两个问题:

  1. alist 只能在您的 Random 方法中访问,因为它是一个局部变量并且是在此处声明的。为了解决这个问题你需要让alist declared with a larger scope here是对局部变量的解释

  2. setText需要String类型输入,而alist不是字符串而是列表。如果你想访问 alist 的元素,那么你可以使用 alist.get(yourIndex);

    static List<Integer> alist; //is not in random method so it can be accessed by other methods
    public static void Random ()
    {
    int Rand [] = new int [49];
    for (int b = 0; b < 49; b++)
        Rand [b] = b;
    
    alist = Arrays.stream(Rand)
                                        .boxed()
                                        .map (x -> x + 1)
                                        .collect (Collectors.toList());                                 
    
    Collections.shuffle(alist);
    }
    
    
    private class HandleTextField implements ActionListener
    {   
       @Override
       public void actionPerformed(ActionEvent event)
       {
           for (int a = 0; a < 49; a++)
           {
            if (event.getSource() == jbArray[a]) 
            {
                jbArray[a].setText(alist.get(a)+"");//uses only the element you want rather than all of the list
            }   
         }
       }
    }
    

希望对您有所帮助!

首先,列表的生成效率不高,您创建一个从 0 到 48 的数组,然后使用流将每个值递增 1,并将结果收集到 List 中以对其进行洗牌。 .

您可以

for(int i = 1; i < 50;  ++i){
    aList.add(i);
}
Collections.shuffle(aList);

如果你真的想要,也可以使用 Stream

List<Integer> list = IntStream.range(1, 50).boxed().collect(Collectors.toList());
Collections.shuffle(list);

然后,您需要使该列表可从操作中访问,您可以

  • 将列表设置为静态或成员变量
  • return 方法中的列表

我更喜欢第二个版本

public static List<Integer> Random(){
    List<Integer> list = IntStream.range(1, 50).boxed().collect(Collectors.toList());
    Collections.shuffle(list);
    return list;
}

完美,您有一个可在该方法中访问的列表。现在您只需迭代每个按钮的每个值并将 Integer 转换为 String。我喜欢串联。

@Override
public void actionPerformed(ActionEvent event)
{
    //Shuffle the values on each buttons
    if(event.getSource() == shuffleButton){
        List<Integer> list = random();
        for(JButton btn : jbArray){
            btn.setText("" + list.remove(0)); //Removing the item is a security to be sure the value will not be used later by mistake.
        }
    }
}

单击按钮 "Shuffle",jbArray 中的每个按钮都将获得自己的值。这可以适应创建动态值长度,比方说,按钮的动态数量。

List<Integer> list = random(jbArray.length);
for(JButton btn : jbArray){
    btn.setText("" + list.remove(0)); //Removing the item is a security to be sure the value will not be used later by mistake.
}

random 变成了:

public static List<Integer> random(int range){
    List<Integer> list = IntStream.range(1, range).boxed().collect(Collectors.toList());
    Collections.shuffle(list);
    reutrn list;
}