How to change jButton value when clicked

别说谁变了你拦得住时间么 提交于 2021-02-08 10:09:38

问题


I kept getting errors while compiling.

I have no idea how to use the list generated integers and display it into the jButtons after clicking.

 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:


So, you have two problems here:

  1. alist is only accessible inside your Random method because it is a local variable and it was declared there. In order to solve this problem you need to make alist declared with a larger scope here is an explanation of local variables.

  2. setText requires a String type input and alist is not a string but a list. If you want to access an element of alist then you can use 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
            }   
         }
       }
    }
    

I hope this helps you!




回答2:


First, the generation of the list is not efficient, You create an array from 0 to 48 then use a stream to increment each values by 1 and collect the result in a List to shuffle it...

You can either

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

Or use a Stream if you really wants

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

Then, you need to make that list accessible from the action, you can either

  • set the list a static or member variable
  • return the list from the method

I prefer the second version

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

Perfect, you have a list accessible in the method. Now you just have to iterate each values for each button and convert the Integer into a String. I like concatenation for that.

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

On one click on the button "Shuffle", every buttons present in jbArray will get there own values. This could be adapted to create a dynamic value length, for let's say, a dynamic number of button.

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

where random became :

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


来源:https://stackoverflow.com/questions/50126848/how-to-change-jbutton-value-when-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!