如何将列表传递给swing中的初始帧

时间:2012-11-13 01:36:42

标签: java swing

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框架中?

3 个答案:

答案 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();
            }
        }
        });
    }
}