我刚刚将示例代码从https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html(响应节点选择)复制到我的中,如下所示
public class IHM_PRO_CHIC extends JFrame implements ActionListener,KeyListener {
public IHM_PRO_CHIC () {
super();
this.addKeyListener(this);
cree_ihm();
};
private void cree_ihm() {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("CHIC");
Tree tree = new Tree (tree_file,top);
JScrollPane treeView = new JScrollPane(tree);
treeView.setVisible(true);
pane.add("Center",treeView);
pane.setVisible(true);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);
};
public void valueChanged(TreeSelectionEvent e) {
//Returns the last path element of the selection.
//This method is useful only when the selection model allows a single selection.
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null)
//Nothing is selected.
return;
Object nodeInfo = node.getUserObject();
if (node.isLeaf()) {
BookInfo book = (BookInfo)nodeInfo;
displayURL(book.bookURL);
} else {
displayURL(helpURL);
}
}
我收到以下错误
error: IHM_PRO_CHIC is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class IHM_PRO_CHIC extends JFrame implements ActionListener,KeyListener {
../ihm_pro_chic/IHM_PRO_CHIC.java:202: error: method addTreeSelectionListener in class JTree cannot be applied to given types;
tree.addTreeSelectionListener(this);
^
required: TreeSelectionListener
found: IHM_PRO_CHIC
reason: actual argument IHM_PRO_CHIC cannot be converted to TreeSelectionListener by method invocation conversion
我的目标是在点击两次节点时启动一个事件。
更新:除了使用接口之外,是否有更快的方式来响应节点点击?