Java 单击时如何更改jButton值
我在编译时不断出错 我不知道如何使用列表生成的整数,并在单击后将其显示到jButtons中Java 单击时如何更改jButton值,java,list,lambda,stream,jbutton,Java,List,Lambda,Stream,Jbutton,我在编译时不断出错 我不知道如何使用列表生成的整数,并在单击后将其显示到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)
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);
}
}
}
}
publicstaticvoidrandom()
{
整数兰德[]=新整数[49];
对于(int b=0;b<49;b++)
兰德[b]=b;
List alist=Arrays.stream(Rand)
.boxed()
.map(x->x+1)
.collect(Collectors.toList());
收藏。洗牌(列表);
}
私有类HandleTextField实现ActionListener
{
@凌驾
已执行的公共无效操作(操作事件)
{
对于(int a=0;a<49;a++)
{
if(event.getSource()==jbArray[a])
{
//此行错误“无法解析为变量”
jbArray[a].setText(alist);
}
}
}
}
因此,这里有两个问题:
静态列表列表//不是随机方法,因此可以通过其他方法访问
公共静态无效随机()
{
整数兰德[]=新整数[49];
对于(int b=0;b<49;b++)
兰德[b]=b;
alist=Arrays.stream(Rand)
.boxed()
.map(x->x+1)
.collect(Collectors.toList());
收藏。洗牌(列表);
}
私有类HandleTextField实现ActionListener
{
@凌驾
已执行的公共无效操作(操作事件)
{
对于(int a=0;a<49;a++)
{
if(event.getSource()==jbArray[a])
{
jbArray[a].setText(alist.get(a)+“”);//只使用所需的元素,而不使用列表中的所有元素
}
}
}
}
我希望这对你有帮助 这里有两个问题:
静态列表列表//不是随机方法,因此可以通过其他方法访问
公共静态无效随机()
{
整数兰德[]=新整数[49];
对于(int b=0;b<49;b++)
兰德[b]=b;
alist=Arrays.stream(Rand)
.boxed()
.map(x->x+1)
.collect(Collectors.toList());
收藏。洗牌(列表);
}
私有类HandleTextField实现ActionListener
{
@凌驾
已执行的公共无效操作(操作事件)
{
对于(int a=0;a<49;a++)
{
if(event.getSource()==jbArray[a])
{
jbArray[a].setText(alist.get(a)+“”);//只使用所需的元素,而不使用列表中的所有元素
}
}
}
}
我希望这对你有帮助 首先,列表的生成效率不高,您创建一个从0到48的数组,然后使用一个流将每个值递增1,并将结果收集到一个
列表
中以将其洗牌
你也可以
for(int i = 1; i < 50; ++i){
aList.add(i);
}
Collections.shuffle(aList);
然后,您需要使该列表可从操作访问,您可以
- 将列表设置为静态变量或成员变量
- 从方法返回列表
public static List<Integer> Random(){
List<Integer> list = IntStream.range(1, 50).boxed().collect(Collectors.toList());
Collections.shuffle(list);
return list;
}
单击按钮“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.
}
首先,列表的生成效率不高,您创建一个从0到48的数组,然后使用一个流将每个值递增1,并将结果收集到
列表中以将其洗牌
你也可以
for(int i = 1; i < 50; ++i){
aList.add(i);
}
Collections.shuffle(aList);
然后,您需要使该列表可从操作访问,您可以
- 将列表设置为静态变量或成员变量
- 从方法返回列表
我更喜欢第二个版本
public static List<Integer> Random(){
List<Integer> list = IntStream.range(1, 50).boxed().collect(Collectors.toList());
Collections.shuffle(list);
return list;
}
单击按钮“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.
}
你能告诉我们错误吗?alist
是Random()
方法的局部错误,这意味着该方法之外的任何东西都不能使用它。好的。单击时,如何使列表中的整数显示在jbutton上?我遇到了一个错误“alist无法解析为变量”。您希望显示alist的单个元素还是整个列表?如果你想要整个列表,你希望它以什么方式显示(例如逗号分隔)?@Devon Rutledge我有49个JButton,我希望每个JButton显示一个从alist
的随机整数,从0到49。你能告诉我们错误吗?alist
是random()方法的局部错误,也就是说,除了这个方法之外,没有任何东西可以使用它。单击时,如何使列表中的整数显示在jbutton上?我遇到了一个错误“alist无法解析为变量”。您希望显示alist的单个元素还是整个列表?如果你想要整个列表,你希望它如何显示(例如逗号分隔)?@Devon Rutledge我有49个按钮,我想要每个按钮
public static List<Integer> random(int range){
List<Integer> list = IntStream.range(1, range).boxed().collect(Collectors.toList());
Collections.shuffle(list);
reutrn list;
}