将数据从ItemListener事件存储到ArrayList中。

huangapple go评论82阅读模式
英文:

Storing data into ArrayList from event in ItemListener

问题

I am quite new to GUI and Swing in Java, please forgive if I asked an easy question.
我对Java中的GUI和Swing还很陌生,请原谅如果我提出了一个简单的问题。
I am currently working on a project of simple cake ordering GUI. I wanted to create multiple JCheckBox with a loop according to the number of toppings available. The check boxes are displaying just right but I can't add the CheckBox text into my toppingOrder arraylist.
我目前正在制作一个简单的蛋糕订购GUI项目。我想根据可用的配料数量使用循环创建多个JCheckBox。复选框显示得很好,但我无法将复选框的文本添加到我的toppingOrder数组列表中。

Tried making another String to store the topping.get(i) but it ended up only storing my last item in topping
尝试创建另一个字符串来存储topping.get(i),但最终只存储了我的最后一个配料项。

And I am getting this error
而且我遇到了这个错误
Local variable i defined in an enclosing scope must be final or effectively final
在封闭作用域中定义的局部变量i必须是final或有效final的。

Also I am a little confused with
另外,我对以下部分有些困惑
topping1.setName("topping" + (i+1));
我从另一篇帖子中找到了这个,但我不太了解它的确切功能。请帮忙。谢谢!

英文:

I am quite new to GUI and Swing in Java, please forgive if I asked an easy question.
I am currently working on a project of simple cake ordering GUI. I wanted to create multiple JCheckBox with a loop according to the number of toppings available. The check boxes are displaying just right but I can't add the CheckBox text into my toppingOrder arraylist.

Tried making another String to store the topping.get(i) but it ended up only storing my last item in topping

for (int i = 0; i < topping.size(); i++) {
			topping1 = new JCheckBox(topping.get(i));
			topping1.setName("topping" + (i+1));
			topping1.setFont(standard);
			topping1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					if (e.getStateChange() == 1) {
						toppingOrder.add(topping.get(i));
					}
				}
			});

And I am getting this error

Local variable i defined in an enclosing scope must be final or effectively final

Also I am a little confused with

topping1.setName("topping" + (i+1));

I got this from another post but i don't really know the exact function of it. Please help. Thankyou!

答案1

得分: 1

这段代码中:

topping1.setName("topping" + (i+1));

只是简单地将复选框的名称设置为字符串 "topping" 与数字相结合(例如 "topping1")。

你遇到的问题与作用域和可见性有关。你正在尝试从匿名内部类中访问本地变量。你可以查看这个答案以获取更多关于如何解决这个问题的信息。

作为一个快速的解决办法,你可以复制你的计数器并在每次运行时重新赋值:

for (int i = 0; i < topping.size(); i++) {
    final int temp = i;
    topping1.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == 1) {
                toppingOrder.add(topping.get(temp));
            }
        }
    });
}

我认为这对你会有用。

英文:

This snippet here:

topping1.setName(&quot;topping&quot; + (i+1));

will simply setthe name of the checkbox by combining the string "topping" with the number (e.g. "topping1").

The issue you are having is due to scope and visibility. You are trying to access your local variable from within an anonymous inner class. You could check this answer for more information on how to solve this.

As a quick fix, what you could do is duplicate your counter and reassign it on every run:

    for (int i = 0; i &lt; topping.size(); i++) {
        final int temp = i;
        topping1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == 1) {
                    toppingOrder.add(topping.get(temp));
                }
            }
        });
    }

I think this would work for you.

答案2

得分: -2

只返回翻译好的部分:

for (final int i = 0; i &lt; topping.size(); i++)
and for topping1.setName you are just setting its name to `&quot;topping1&quot;` when i = 0 
and `&quot;topping2&quot;` when i = 1 and so on and so forth.
英文:
for (final int i = 0; i &lt; topping.size(); i++)

and for topping1.setName you are just setting its name to &quot;topping1&quot; when i = 0
and &quot;topping2&quot; when i = 1 and so on and so forth.

huangapple
  • 本文由 发表于 2020年8月3日 15:50:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63225639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定