当前位置: 代码迷 >> Eclipse >> 还是一种树形结构解决方法
  详细解决方案

还是一种树形结构解决方法

热度:72   发布时间:2016-04-23 14:04:08.0
还是一种树形结构
有一组树形结构的数据,即它有若干个大类,每个大类中有若干个中类,每个中类中有若干个小类,每个小类中可能又有若干个项目......。我想逐层展开它。
每一个大类的左侧显示一个加号,当鼠标点击加号时,便展开其下一层数据(即中类),当中类下面还有更深一层数据(即小类)时,便在该中类左侧显示一个加号,可以接受鼠标选择;若已全部展开则显示一个减号。
想到了treeviews来做,但是没有思路,大神能否给个思路,有例子更好。

------解决方案--------------------
没做过 百度试试吧
------解决方案--------------------
SWT中TREE用起来很繁杂,很不好用,感觉不好。
------解决方案--------------------
你什么时候有时间?我现在没时间,我可以给你列子,身边没有参考资料?
------解决方案--------------------
Java code
import java.awt.EventQueue;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;import javax.swing.JTree;import javax.swing.SpringLayout;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.TreePath;public class myTreeTest extends JFrame {    private JTextField textField;    private JTree tree;    private SpringLayout springLayout;    /**     * Launch the application     * @param args     */    public static void main(String args[]) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    myTreeTest frame = new myTreeTest();                    frame.setVisible(true);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }    /**     * Create the frame     */    public myTreeTest() {        super();        setTitle("维护企业架构");        springLayout = new SpringLayout();        getContentPane().setLayout(springLayout);        setBounds(100, 100, 313, 485);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        tree = new JTree();        getContentPane().add(tree);        springLayout.putConstraint(SpringLayout.SOUTH, tree, 310, SpringLayout.NORTH, getContentPane());        springLayout.putConstraint(SpringLayout.NORTH, tree, 25, SpringLayout.NORTH, getContentPane());        springLayout.putConstraint(SpringLayout.EAST, tree, 255, SpringLayout.WEST, getContentPane());        springLayout.putConstraint(SpringLayout.WEST, tree, 50, SpringLayout.WEST, getContentPane());        final JLabel label = new JLabel();        label.setText("名称:");        getContentPane().add(label);        springLayout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, getContentPane());        textField = new JTextField();        getContentPane().add(textField);        springLayout.putConstraint(SpringLayout.NORTH, textField, 375, SpringLayout.NORTH, getContentPane());        final JButton btnadd = new JButton();        btnadd.setText("添加");        btnadd.addActionListener(new ActionListener(){            @Override            public void actionPerformed(ActionEvent e) {                // TODO Auto-generated method stub                DefaultMutableTreeNode addnode=new DefaultMutableTreeNode(textField.getText());                TreePath selpath=tree.getSelectionPath();                DefaultMutableTreeNode parentnode=(DefaultMutableTreeNode) selpath.getLastPathComponent();                DefaultTreeModel treeModel=new DefaultTreeModel(addnode);                treeModel.insertNodeInto(addnode, parentnode, parentnode.getChildCount());                TreePath path=selpath.pathByAddingChild(addnode);                if(!tree.isVisible(path)){                    tree.makeVisible(path);                }            }                    });        getContentPane().add(btnadd);        springLayout.putConstraint(SpringLayout.EAST, btnadd, 260, SpringLayout.WEST, getContentPane());        springLayout.putConstraint(SpringLayout.WEST, btnadd, 190, SpringLayout.WEST, getContentPane());        springLayout.putConstraint(SpringLayout.SOUTH, btnadd, 355, SpringLayout.NORTH, getContentPane());        final JButton btndel = new JButton();        btndel.setText("删除");        btndel.addActionListener(new ActionListener(){            @Override            public void actionPerformed(ActionEvent e) {                // TODO Auto-generated method stub                DefaultMutableTreeNode delnode=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();//选中欲删除的节点                if(!delnode.isRoot()){                    DefaultMutableTreeNode nextselnode=delnode.getNextSibling();                    if(nextselnode==null){                        nextselnode=(DefaultMutableTreeNode) delnode.getParent();                    }                    DefaultTreeModel deltreeModel=new DefaultTreeModel(delnode);                    deltreeModel.removeNodeFromParent(delnode);                    tree.setSelectionPath(new TreePath(nextselnode.getPath()));                }            }                    });        getContentPane().add(btndel);        springLayout.putConstraint(SpringLayout.EAST, textField, -5, SpringLayout.WEST, btndel);        springLayout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);        springLayout.putConstraint(SpringLayout.SOUTH, label, 0, SpringLayout.SOUTH, btndel);        springLayout.putConstraint(SpringLayout.EAST, btndel, 0, SpringLayout.EAST, btnadd);        springLayout.putConstraint(SpringLayout.WEST, btndel, 0, SpringLayout.WEST, btnadd);        springLayout.putConstraint(SpringLayout.SOUTH, btndel, 395, SpringLayout.NORTH, getContentPane());        final JButton btnup = new JButton();        btnup.setText("修改");        btnup.addActionListener(new ActionListener(){            @Override            public void actionPerformed(ActionEvent e) {                // TODO Auto-generated method stub                                TreePath selpath=tree.getSelectionPath();                DefaultMutableTreeNode upnode=(DefaultMutableTreeNode) selpath.getLastPathComponent();                upnode.setUserObject(textField.getText());                DefaultTreeModel uptreeModel=new DefaultTreeModel(upnode);                uptreeModel.nodeChanged(upnode);                tree.setSelectionPath(selpath);            }                    });        getContentPane().add(btnup);        springLayout.putConstraint(SpringLayout.EAST, btnup, 75, SpringLayout.EAST, textField);        springLayout.putConstraint(SpringLayout.WEST, btnup, 5, SpringLayout.EAST, textField);        springLayout.putConstraint(SpringLayout.NORTH, btnup, 405, SpringLayout.NORTH, getContentPane());        //            }}
  相关解决方案