单击时如何更改jButton值 [英] How to change jButton value when clicked
本文介绍了单击时如何更改jButton值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在编译时不断出错.
我不知道如何使用列表生成的整数,并在单击后将其显示在jButton中.
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);
}
}
}
}
推荐答案
因此,您在这里遇到两个问题:
So, you have two problems here:
-
alist仅可在您的Random方法内部访问,因为它是一个局部变量,并且已在其中声明.为了解决此问题,您需要使用更大范围声明的列表这里是局部变量的说明.
setText需要输入字符串类型,而列表不是字符串而是列表.如果要访问alist的元素,则可以使用alist.get(yourIndex);
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
}
}
}
}
希望对您有帮助!
这篇关于单击时如何更改jButton值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文