不知道楼主的事件引用是怎么作的。应该是treeExpanded方法中不管作了什么动作,都会回去展开树(除异常外)。

解决方案 »

  1.   

    应该添加:
    tree.addTreeWillExpandListener( new TreeWillExpandListener() {
            public void treeWillCollapse(TreeExpansionEvent event) {//折叠
            }        public void treeWillExpand(TreeExpansionEvent event) {//打开
                    try{
               String PASSWORD="manager";
               String VALUE=JOptionPane.showInputDialog(this,"请输入密码:");
               if(VALUE.compareToIgnorCase(PASSWORD)!=0){
                   JOptionPane.showMessageDialog(this,"密码错误");
                   return;
           }
           catch(Exception exp){
               exp.getMessage();
           }        }
    } );
      

  2.   

    tomcatjava(小鱼儿) :用addTreeWillExpandListener还是不行啊,要是密码错误,一样的会展开,有不有什么方法让树不展开呢?
      

  3.   

    tree.addTreeWillExpandListener( new TreeWillExpandListener() {
            public void treeWillCollapse(TreeExpansionEvent event) {//折叠
            }        public void treeWillExpand(TreeExpansionEvent event) {//打开
                    try{
               String PASSWORD="manager";
               String VALUE=JOptionPane.showInputDialog(this,"请输入密码:");
               if(VALUE.compareToIgnorCase(PASSWORD)!=0){
                   TreePath path = event.getPath();
                   tree.setExpandedState( path,false );//父亲节点可展开,但孩子节点都不行               JOptionPane.showMessageDialog(this,"密码错误");
                   return;
           }
           catch(Exception exp){
               exp.getMessage();
           }        }
    } );
      

  4.   

    可能需要再加一个:
    if( VALUE.compareToIgnorCase(PASSWORD)!=0 ){
            //看上面的
    }
    else {
            TreePath path = event.getPath();
            tree.setExpandedState( path,true );//还需要重置这个状态值
    }
      

  5.   

    楼上兄弟,setExpandedState()方法是protect修饰啊,在编译的时候通不过啊,怎样才能正确引用setExpandedState()呢?
      

  6.   

    下面是可执行的,自己看看吧(花了我些时间呀):import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.tree.*;
    import javax.accessibility.*;
    import java.beans.*;
    import javax.swing.JTree;/**
    This program shows a simple tree.
    */
    public class SimpleTree
    {  
    public static void main(String[] args)
    {  
       JFrame frame = new SimpleTreeFrame();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.show();
    }
    }/**
    This frame contains a simple tree that displays a 
    manually constructed tree model.
    */
    class SimpleTreeFrame extends JFrame
    {  
    private JTree tree = null;
    private boolean mutex = true;public SimpleTreeFrame()
    {  
       setTitle("SimpleTree");
       setSize(WIDTH, HEIGHT);   // set up tree model data   DefaultMutableTreeNode root
          = new DefaultMutableTreeNode("World");
       DefaultMutableTreeNode country
          = new DefaultMutableTreeNode("USA");
       root.add(country);
       DefaultMutableTreeNode state
          = new DefaultMutableTreeNode("California");
       country.add(state);
       DefaultMutableTreeNode city
          = new DefaultMutableTreeNode("San Jose");
       state.add(city);
       city = new DefaultMutableTreeNode("Cupertino");
       state.add(city);
       state = new DefaultMutableTreeNode("Michigan");
       country.add(state);
       city = new DefaultMutableTreeNode("Ann Arbor");
       state.add(city);
       country = new DefaultMutableTreeNode("Germany");
       root.add(country);
       state = new DefaultMutableTreeNode("Schleswig-Holstein");
       country.add(state);
       city = new DefaultMutableTreeNode("Kiel");
       state.add(city);   // construct tree and put it in a scroll pane tree = new JTree(root);
    //tree. tree.addTreeWillExpandListener( new TreeWillExpandListener() {
    public void treeWillCollapse(TreeExpansionEvent event) {//折叠
    } public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {//打开
                  boolean value=false;
    String PASSWORD = "manager";
    String VALUE = JOptionPane.showInputDialog( null,"请输入密码:","提示",JOptionPane.OK_OPTION );

    if( VALUE != null ) {
    if(VALUE.equalsIgnoreCase(PASSWORD)){
    value=true;
    }
    if( !VALUE.equalsIgnoreCase(PASSWORD) ){
    JOptionPane.showMessageDialog( null,"密码错误","提示",JOptionPane.OK_OPTION );
    }
    }
    else {
    value = false;
    }
    if(value == false){
    throw new ExpandVetoException(event);
    }
                 }
    } );   Container contentPane = getContentPane();
       contentPane.add(new JScrollPane(tree));
    }private static final int WIDTH = 300;
    private static final int HEIGHT = 200;
    }