//以前从网上找到的,别人的成果。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.tree.*;
import java.awt.event.*;
public class TreeWindow extends JFrame implements TreeSelectionListener {
JSplitPane jsp = new JSplitPane();
JScrollPane jop = new JScrollPane();
WelcomePane wp = new WelcomePane();
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("本地系统处理模块");
JTree jt = new JTree();
TreeWindow() {
super("流行的Tree窗口示例");
setSize(600, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(jsp);
jsp.setLeftComponent(jop);
jsp.setRightComponent(wp);
jop.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jop.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jop.getViewport().setView(jt);
// jsp.add(jt);
initTree();
jt.expandRow(0);
jt.expandRow(1);
jt.expandRow(6);
jt.addTreeSelectionListener(this);
setVisible(true);
}
void initTree() {
DefaultMutableTreeNode childNode1 = new DefaultMutableTreeNode("项目监视");
rootNode.add(childNode1);
DefaultMutableTreeNode childNode2 = new DefaultMutableTreeNode("项目计划");
// rootNode.add(childNode2);
childNode1.add(childNode2);
childNode2 = new DefaultMutableTreeNode("项目核定");
childNode1.add(childNode2);
childNode2 = new DefaultMutableTreeNode("每日巡视");
childNode1.add(childNode2);
childNode2 = new DefaultMutableTreeNode("进展汇报");
childNode1.add(childNode2);
//
childNode1 = new DefaultMutableTreeNode("项目核算");
rootNode.add(childNode1);
childNode2 = new DefaultMutableTreeNode("成本录入");
childNode1.add(childNode2);
childNode2 = new DefaultMutableTreeNode("成本复核");
childNode1.add(childNode2);
childNode2 = new DefaultMutableTreeNode("帐目阅览");
childNode1.add(childNode2);
}
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jt.getLastSelectedPathComponent();
if (selectedNode.toString() == "项目计划") {
PlanPane pp = new PlanPane();
jsp.setRightComponent(pp);
} else if (selectedNode.toString() == "项目核定") {
PlanCheck pc = new PlanCheck();
jsp.setRightComponent(pc);
}
}
public static void main(String[] args) {
TreeWindow tw = new TreeWindow();
}
}
class WelcomePane extends JPanel {
JLabel jl = new JLabel("生产项目管理系统");
WelcomePane() {
add(jl);
}
}
class PlanPane extends JPanel implements ActionListener {
JButton jb = new JButton("项目计划");
PlanPane() {
jb.addActionListener(this);
add(jb);
}
public void actionPerformed(ActionEvent ed) {
if (ed.getSource() == jb)
System.out.println("项目计划按纽被单击");
}
}
class PlanCheck extends JPanel implements ActionListener
{
JButton jb = new JButton("项目核定");
PlanCheck() {
jb.addActionListener(this);
add(jb);
}
public void actionPerformed(ActionEvent eg) {
if (eg.getSource() == jb)
System.out.println("项目核定按纽被单击");
}
}