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() でこれを行うにはどうすればよいですか?

エラーが発生します:

別のメソッドで定義された内部クラス内の非最終変数「items」を参照できません

エラーは、 MainFrame frame = new MainFrame(items) MainFrame クラスに項目変数を渡すことができないようです... どうしてですか?

この変数を MainFrame フレームに渡すにはどうすればよいですか?

4

3 に答える 3

4

あなたには2つの選択肢があります....

選択肢 1...

itemsリストを final にして、s コンテキスト内からアクセスできるようにします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リストを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最終宣言する必要があります。

を拡張する内部クラスから非最終ローカル変数にアクセスしていますが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 に答える