繁体   English   中英

单击时如何更改jButton值

[英]How to change jButton value when clicked

我在编译时不断出错。

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

 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方法内部访问,因为它是一个局部变量,并且已在其中声明。 为了解决此问题,您需要使声明的列表具有更大的作用域, 这里是对局部变量的解释。

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

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

  • 将列表设置为静态或成员变量
  • 从方法返回列表

我更喜欢第二版

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.
        }
    }
}

只需单击按钮“随机播放”, jbArray存在的每个按钮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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM