下面是可执行的,自己看看吧(花了我些时间呀):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; }
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();
} }
} );
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();
} }
} );
if( VALUE.compareToIgnorCase(PASSWORD)!=0 ){
//看上面的
}
else {
TreePath path = event.getPath();
tree.setExpandedState( path,true );//还需要重置这个状态值
}
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;
}