gpt4 book ai didi

java - 在循环中调用 SwingUtilities.invokeAndWait

转载 作者:行者123 更新时间:2023-11-30 08:24:04 27 4
gpt4 key购买 nike

我正在尝试保存 JFrame 的 .PNG 图像。在 JFrame 中,我有两个 Plot3DPanel 对象。很可能是因为同步错误,在保存 .PNG 时左侧尺寸的框架是不可见的。

enter image description here

因此,我使用以下代码:

try
{
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new VisualSerializer(P[i], Q[i], folder + "C" + i).run();
}
});

serialize(P[i], folder + "P" + i);
serialize(Q[i], folder + "P" + i);

}
catch(Exception e)
{
e.printStackTrace();
}

但是现在,我得到了编译错误

Cannot refer to a non-final variable i inside an inner class defined in a different method

但是,我需要不同的文件名。另外,我需要遍历我的对象数组。我该怎么做才能避免此错误?

最佳答案

一种方法是制作任何需要传递到匿名内部类的变量的最终副本,例如,如果 i 是问题所在:

final int index = i;
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new VisualSerializer(P[index], Q[index], folder + "C" + index).run();
}
});

另一种解决方案是在匿名内部类中使用类字段。

关于java - 在循环中调用 SwingUtilities.invokeAndWait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23201275/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com