如何解决这个问题?
错误在于@scrollPane和map。
scrollPane 错误:无法分配最终局部变量,因为它是在封闭类型中定义的
map 错误建议:初始化变量。
final Map<ItemType, JScrollPane> viewScrollPanes = new HashMap<ItemType, JScrollPane>();
final JScrollPane scrollPane;
final Map<ItemType, JScrollPane> map;
this.viewList.forEach((type, list) -> {
list.setSelectionMode(0);
list.setVisibleRowCount(5);
list.putClientProperty("type", type);
list.setCellRenderer(this.itemRenderer);
list.setName(String.valueOf(type.toString()) + "List");
scrollPane = new JScrollPane(list);
scrollPane.setMinimumSize(new Dimension(0, 110));
scrollPane.setName(type.toString());
map.put(type, scrollPane);
return;
});
最佳答案
您不能在循环中重复分配给 final
变量。
看起来 scrollPane
应该是每次迭代内部的局部变量。
map
需要在进入循环之前初始化(否则你无法在其中放入
任何内容)。
关于Java:最终局部变量无法分配,因为它是在封闭类型中定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191657/