我对内部类访问有疑问。我没有Java经验,所以请耐心等待。
以下是我写的代码:
public class MainFrame extends JFrame {
...
private String selectedNodeString = NULL; //outer class variable
private JPanel createControlPanel() {
...
parseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
tree.addTreeSelectionListener(new MyTreeSelectionListener());
}
});
class MyTreeSelectionListener implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent e) {
selectedNodeString = //compile error, can not resolve type
}
}
}
以下是来自"思考java"的一个例子。解释内部类访问,其中内部类可以访问外部类变量。
interface Selector {
...
}
public class Sequence {
private Object[] items;
private class SequenceSelector implements Selector {
...
private int i = 0;
public boolean end() { return i == items.length; }
public Object current() { return items[i]; }
public void next() { if(i < items.length) i++; }
}
}
我的代码有什么问题,它是不是超过1层内部类,以及如何修复它?
由于
答案 0 :(得分:0)
您在方法createControlpanel之后缺少大括号。
private JPanel createControlPanel() {
...
parseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
tree.addTreeSelectionListener(new MyTreeSelectionListener());
}
});
} // missing this one.