0
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() 中执行此操作?

我得到错误:

无法引用以不同方法定义的内部类中的非最终变量“项目”

错误在于 MainFrame frame = new MainFrame(items) 我似乎无法将 MainFrame 类传递给 items 变量...这是为什么呢?

如何将此变量传递到 MainFrame 框架中?

4

3 回答 3

4

你有两个选择......

选择一...

items列表设为最终列表,以便可以从Runnables 上下文中访问它...

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列表移动到Runnables 上下文中

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();
            }
        }
    });
}
于 2012-11-13T01:49:52.470 回答
2

List<Item> items需要声明为final。

您正在从扩展的内部类访问非最终局部变量Runnable,这是不允许的。

于 2012-11-13T01:42:15.407 回答
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();
            }
        }
        });
    }
}
于 2012-11-13T01:45:47.557 回答