线程“AWT-EventQueue-0”中出现异常java.util

发布于 2024-08-22 12:17:41 字数 353 浏览 12 评论 0原文

我该如何修复此代码?我不知道这个错误是什么意思...我听说它来自于在 foreach 循环期间删除了列表的元素,但我没有看到我要删除的任何内容...

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        for(Layer e : layerList)
            e.drawLayer(g2);
    }

jcomponent 有一个列表称为图层的对象,它将 Graphics 传递给这些对象,以便图层可以自行绘制。我从不删除任何层或任何东西,所以我迷失了。帮助?

How do I fix this code? i don't know what this error means... I heard that it comes from having elements of a list removed during a for each loop, but I don't see anything tha I'm removing...

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        for(Layer e : layerList)
            e.drawLayer(g2);
    }

The jcomponent has a list of objects called layers that it passes Graphics to so the layers can paint themselves. I never remove any of the layers or anything, so I'm lost. Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2024-08-29 12:17:41

如果您有多个线程可以处理图层列表,则应考虑使用同步块作为下面的示例。这将有助于防止此问题,或者您可以考虑同步 LayerList,但如果没有程序和线程结构的更多信息,很难告诉您什么是最好的。查看 此同步列表作为选项而不是同步块。

 synchronized( layerList ) 
 {
    for(Layer e : layerList)
        e.drawLayer(g2);
 }

If you have more then one thread working that could be working with the layerlist, you should consider using a synchronize block as an example below. That will help prevent this problem or you could consider having the layerList be synchronized but with out more info of the program and the thread structure it is hard to tell you what is best. Check out this synchronized list as an option instead of the synchronized block.

 synchronized( layerList ) 
 {
    for(Layer e : layerList)
        e.drawLayer(g2);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文