【问题标题】:How to change jButton value when clicked单击时如何更改jButton值
【发布时间】:2018-05-02 03:45:01
【问题描述】:

我在编译时不断出错。

我不知道如何使用列表生成的整数并在单击后将其显示到 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);
        }   
     }
   }
} 

【问题讨论】:

  • 你能告诉我们错误吗?
  • alist 是 Random() 方法的本地,这意味着该方法之外的任何东西都不能使用它。
  • 好的。单击时如何让 alist 中的整数显示在 jbutton 上?我收到此错误“alist 无法解析为变量”
  • 您要显示 alist 的单个元素还是显示整个列表?如果您想要整个列表十,您希望它如何显示(例如逗号分隔)?
  • @Devon Rutledge 我有 49 个 jButton,我希望每个 jButton 显示一个从 alist 的随机整数,即 0 到 49。

标签: java list lambda stream jbutton


【解决方案1】:

所以,这里有两个问题:

  1. alist 只能在您的 Random 方法中访问,因为它是一个局部变量并且在那里声明。为了解决这个问题你需要让alist声明更大的范围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
            }   
         }
       }
    }
    

希望对你有帮助!

【讨论】:

  • Random 是静态方法,所以aList 必须也是。
  • 哎呀,抱歉,现在应该修复了。
  • 是的。它有效!非常感谢你的帮助。我真的很感激
【解决方案2】:

首先,列表的生成效率不高,您创建一个从 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 中的每个按钮都会获得自己的值。这可以适应创建动态值长度,例如,按钮的动态数量。

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

【讨论】:

    猜你喜欢
    • 2014-11-30
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2012-01-30
    • 1970-01-01
    相关资源
    最近更新 更多