public Lab7(File file) {
List<Item> items = null;
try {
items = InventoryReader.read(file);
} catch (ApplicationException e) {
LOG.error(e.getMessage());
return;
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, use the default.
}
try {
MainFrame frame = new MainFrame(items); // THIS IS WHERE I HAVE TROUBLE
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
背景:我将MainFrame的构造函数设置为接受列表。我如何在我的应用程序的main()中执行此操作?
我收到错误:
不能引用在不同方法中定义的内部类中的非final变量“items”
错误出现在 MainFrame frame = new MainFrame(items)
我似乎无法传递MainFrame类的items变量......为什么会这样?
如何将此变量传递到MainFrame框架中?
答案 0 :(得分:4)
你有两个选择......
将items
列表设为最终,以便可以在Runnable
的上下文中进行访问...
public Lab7(File file) {
final List<Item> items = null; // Make the items final...
try {
items = InventoryReader.read(file);
} catch (ApplicationException e) {
LOG.error(e.getMessage());
return;
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, use the default.
}
try {
MainFrame frame = new MainFrame(items); // THIS IS WHERE I HAVE TROUBLE
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
将items
列表移至Runnable
上下文
public Lab7(File file) {
EventQueue.invokeLater(new Runnable() {
public void run() {
// Load the items within the content of the runnable...
List<Item> items = null;
try {
items = InventoryReader.read(file);
} catch (ApplicationException e) {
LOG.error(e.getMessage());
return;
}
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, use the default.
}
try {
MainFrame frame = new MainFrame(items); // THIS IS WHERE I HAVE TROUBLE
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
答案 1 :(得分:2)
List<Item> items
需要宣布为最终版。
您正在从扩展Runnable
的内部类中访问非最终的本地变量,这是不允许的。
答案 2 :(得分:2)
将List<Item> items
声明为成员变量,并使用构造函数初始化列表。
public class Lab7 {
private List<Item> items;
public Lab7(File file) {
try {
items = InventoryReader.read(file);
} catch (ApplicationException e) {
LOG.error(e.getMessage());
return;
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, use the default.
}
try {
MainFrame frame = new MainFrame(items);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}