Swing

    技术2022-05-11  7

     package treeTest;

    //13225678980import java.awt.*;

    import javax.swing.*;import javax.swing.tree.*;import javax.swing.event.*;import java.awt.Rectangle;import java.awt.event.*;import java.io.File;

    public class treeTest extends JFrame {    DefaultMutableTreeNode modelTree;    JScrollPane jScrollPane1 = new JScrollPane();    JTree tree_t;    JLabel jLabel1 = new JLabel();    JLabel jLabel2 = new JLabel();    JLabel lbl_parents = new JLabel();    JButton btn_add = new JButton();    JButton btn_delete = new JButton();    JButton btn_clear = new JButton();    DefaultTreeCellEditor tce;    ImageIcon secondkinds = new ImageIcon(treeTest.class.getResource(            "bullet.gif"));    JTextField jtext_ower = new JTextField();    public treeTest() {        try {            jbInit();        } catch (Exception exception) {            exception.printStackTrace();        }    }

        private void inintree(DefaultMutableTreeNode model, File file) {        DefaultMutableTreeNode tmep = new DefaultMutableTreeNode(file.getName());        model.add(tmep);        if (file.isDirectory()) {            File[] tempfile = file.listFiles();            if (tempfile.length != 0) {                for (int i = 0; i < tempfile.length; i++) {                    inintree(tmep, tempfile[i]);                }            }        }    }

        private void jbInit() throws Exception {        File[] file = File.listRoots();        modelTree = new DefaultMutableTreeNode(file[2].getPath());        File[] file1 = file[2].listFiles();        for (int i = 0; i < file1.length; i++) {            inintree(modelTree, file1[i]);        }        tree_t = new JTree(modelTree);        DefaultTreeCellRenderer render = new DefaultTreeCellRenderer();        render.setLeafIcon(secondkinds);        render.setFont(new Font("宋体", 0, 10));        tree_t.setCellRenderer(render);        tree_t.setOpaque(true);        getContentPane().setLayout(null);        jScrollPane1.setBounds(new Rectangle(10, 15, 141, 227));        lbl_parents.setFont(new java.awt.Font("宋体", Font.BOLD, 16));        btn_add.setBounds(new Rectangle(175, 180, 61, 25));        btn_add.setText("增加");        btn_delete.setBounds(new Rectangle(247, 179, 63, 26));        btn_delete.setText("删除");        btn_clear.setBounds(new Rectangle(322, 180, 60, 25));        btn_clear.setToolTipText("");        btn_clear.setText("清空");        this.getContentPane().setBackground(SystemColor.control);        jtext_ower.setBounds(new Rectangle(238, 95, 96, 27));        jtext_ower.addKeyListener(new KeyAdapter() {            public void keyPressed(KeyEvent e) {                jtext_ower_keyPressed(e);            }        });

            this.getContentPane().add(jScrollPane1);        jScrollPane1.getViewport().add(tree_t);        lbl_parents.setBounds(new Rectangle(239, 25, 124, 26));        jLabel2.setFont(new java.awt.Font("宋体", Font.BOLD, 16));        jLabel2.setToolTipText("");        jLabel2.setText("本目录");        jLabel2.setBounds(new Rectangle(174, 95, 58, 20));        this.getContentPane().add(jLabel1);        this.getContentPane().add(lbl_parents);        this.getContentPane().add(jLabel2);        this.getContentPane().add(btn_add);        this.getContentPane().add(btn_delete);        this.getContentPane().add(btn_clear);        this.getContentPane().add(jtext_ower);        jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 16));        jLabel1.setToolTipText("");        jLabel1.setText("父目录");        jLabel1.setBounds(new Rectangle(174, 24, 59, 27));        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();        this.setSize(400, 300);        this.setLocation((screen.width - 300) / 2, (screen.height - 300) / 2);        this.setVisible(true);       /* tree_t.setEditable(true);        tce = (DefaultTreeCellEditor) tree_t.getCellEditor();*/        tree_t.addTreeSelectionListener(new TreeSelectionListener() {            public void valueChanged(TreeSelectionEvent e) {                TreePath tp = tree_t.getSelectionPath();                DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) tp.                                              getLastPathComponent();                if (dmtn.isRoot()) {                    lbl_parents.setText("");                    jtext_ower.setText(dmtn.toString());                } else {                    DefaultMutableTreeNode topdmtn = (DefaultMutableTreeNode)                            tp.getParentPath().getLastPathComponent();                    lbl_parents.setText(topdmtn.toString());                    jtext_ower.setText(dmtn.toString());                }            }        });        btn_add.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                addNode();            }        });        btn_delete.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                delete();            }        });        btn_clear.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                modelTree.removeAllChildren();                tree_t.updateUI();            }        });    }

        private void delete() {        TreePath treepath = tree_t.getSelectionPath();        DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) treepath.                                      getLastPathComponent();        if (dmtn.isRoot()) {            JOptionPane.showMessageDialog(this, "根目录不能删除");        } else if (dmtn.getChildCount() != 0) {            JOptionPane.showMessageDialog(this, "文件架非空,不能删除");        } else {            DefaultMutableTreeNode top = (DefaultMutableTreeNode) tree_t.                                         getSelectionPath().getParentPath().                                         getLastPathComponent();            top.remove(dmtn);            tree_t.updateUI();        }    }

        private void addNode() {        String TreeNodeName = null;        if (!((TreeNodeName = JOptionPane.showInputDialog(this, "请输入接点名称", "",                JOptionPane.YES_OPTION)) == null)) {            if (TreeNodeName.trim().length() != 0) {                DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode(                        TreeNodeName);                if (tree_t.getSelectionCount() != 0) {                    tree_t.setEditable(true);                    TreePath treepath = tree_t.getSelectionPath();                    DefaultMutableTreeNode topModelNode = (                            DefaultMutableTreeNode) treepath.                            getLastPathComponent();                    topModelNode.insert(dmtn, topModelNode.getChildCount());                    tree_t.updateUI();                }            }        }    }

        public static void main(String[] args) {        treeTest treetest = new treeTest();    }

        public void jtext_ower_keyPressed(KeyEvent e) {        TreePath tp = tree_t.getSelectionPath();        DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) tp.                                      getLastPathComponent();        dmtn.setUserObject(jtext_ower.getText());        tree_t.updateUI();

        }}


    最新回复(0)