java中的继承问题

Ami*_*mir 0 java inheritance swing

我有一个Frame类,我在其上放了一个Jtree,现在我为我的树开发了一个TreeSelectionListener.既然我的树在我的Frame类中,我怎么能访问我的树?

我想知道用户按下的女巫节点.我从我的框架类创建一个stataic类,但我认为这种方式是错误的,请指教我.

public Frame1(){
    JTree jtree = new Jtree();
    public static Frame1 THIS;
     public Frame(){
     init();
     THIS = this;
     }
     public static getTHIS(){
         return THIS;
     }
}

public class jtreeSelectionListener implements TreeSelectionListener{

//here I need to access my jtree object, what should I do in this case?
// now I do like this . Frame1.getTHIS.jtree ...
}
Run Code Online (Sandbox Code Playgroud)

Whi*_*g34 5

只要创建一个构造JTreeSelectionListener是需要你JTree:

public class Frame1 extends JFrame {
    private JTree jtree = new JTree();

    public Frame1() {
        jtree.addTreeSelectionListener(new JTreeSelectionListener(jtree));
    }
}

public class JTreeSelectionListener implements TreeSelectionListener {
    private JTree jtree;

    public JTreeSelectionListener(JTree jtree) {
        this.jtree = jtree;
    }

    public void valueChanged(TreeSelectionEvent e) {
    }
}
Run Code Online (Sandbox Code Playgroud)